TechExplainer / app.py
yasserrmd's picture
Update app.py
ad72b3e verified
# Import necessary libraries
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import spaces
tokenizer = AutoTokenizer.from_pretrained("tiiuae/Falcon3-7B-Instruct")
model = AutoModelForCausalLM.from_pretrained("tiiuae/Falcon3-7B-Instruct", torch_dtype=torch.bfloat16, device_map="auto")
@spaces.GPU
def generate_text(prompt, max_length, temperature, category):
category_prompts = {
"Elder-Friendly": "Explain this concept step-by-step in very simple and clear terms, avoiding any technical jargon or complex words, so that seniors can easily understand: ",
"Kid-Friendly": "Break down this concept into a fun, story-like explanation using simple words and examples that children can relate to and enjoy: ",
"Teen-Friendly": "Make this concept relatable, engaging, and a bit entertaining for teenagers by using examples from pop culture, games, or their daily lives: ",
"Beginner Coders": "Teach this concept as if you are explaining it to someone completely new to programming, using clear analogies and real-world coding examples: ",
"Non-Techies": "Simplify this concept into very clear and plain language, avoiding technical terms while using examples that are easy for a non-technical audience to relate to: ",
"Visual Thinkers": "Use descriptive analogies, mental imagery, and comparisons to help visualize this concept clearly in an easy-to-grasp manner: ",
"Busy Professionals": "Summarize this concept briefly and concisely, focusing only on the essential details to save time, while keeping it professional and clear: ",
"Curious Learners": "Explain this concept in detail, diving into its meaning, examples, and practical relevance, while maintaining clarity and flow: ",
"Tech Enthusiasts": "Provide an insightful and technical explanation of this concept, including its relevance, practical applications, and deeper implications in the tech world: ",
"Educators": "Frame this concept as a teaching guide, providing step-by-step clarity and examples that would be helpful for explaining it to a classroom or audience: ",
"Business Leaders": "Explain this concept from a strategic perspective, focusing on its business relevance, use cases, and real-world value in a professional setting: ",
"Problem Solvers": "Describe this concept with a problem-solving mindset, focusing on practical applications, benefits, and how it can be applied to resolve challenges: "
}
# Prepend the category-specific prompt
category_prompt = category_prompts.get(category, "")
full_prompt = category_prompt + prompt
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_length=max_length,
temperature=temperature
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
generated_text = generated_text.replace(category_prompt, "")
print(generated_text)
return generated_text
# Gradio app interface with input and output components
with gr.Blocks() as demo:
gr.Markdown("#Tech Explainer\nEnter a concept, select a category, and Falcon 3-7B-Instruct will generate a simplified explanation!")
with gr.Row():
prompt_input = gr.Textbox(label="Enter your concept here", lines=3, placeholder="Type something...")
with gr.Row():
category_input = gr.Dropdown([
"Elder-Friendly", "Kid-Friendly", "Teen-Friendly",
"Beginner Coders", "Non-Techies", "Visual Thinkers",
"Busy Professionals", "Curious Learners",
"Tech Enthusiasts", "Educators",
"Business Leaders", "Problem Solvers"
], label="Select Audience Category", value="Elder-Friendly")
with gr.Row():
max_length = gr.Slider(50, 1500, value=750, step=30, label="Max Length")
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
with gr.Row():
generate_button = gr.Button("Generate Explanation")
with gr.Row():
gr.Markdown("Generated Explanation")
with gr.Row():
output = gr.Markdown("""
.
.
.
.
.
.
""")
generate_button.click(generate_text, inputs=[prompt_input, max_length, temperature, category_input], outputs=output)
demo.launch()