Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("tiiuae/Falcon3-7B-Instruct")
|
9 |
+
model = AutoModelForCausalLM.from_pretrained("tiiuae/Falcon3-7B-Instruct", torch_dtype=torch.bfloat16, device_map="auto")
|
10 |
+
|
11 |
+
@spaces.GPU
|
12 |
+
def generate_text(prompt, max_length, temperature, category):
|
13 |
+
category_prompts = {
|
14 |
+
"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: ",
|
15 |
+
"Kid-Friendly": "Break down this concept into a fun, story-like explanation using simple words and examples that children can relate to and enjoy: ",
|
16 |
+
"Teen-Friendly": "Make this concept relatable, engaging, and a bit entertaining for teenagers by using examples from pop culture, games, or their daily lives: ",
|
17 |
+
"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: ",
|
18 |
+
"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: ",
|
19 |
+
"Visual Thinkers": "Use descriptive analogies, mental imagery, and comparisons to help visualize this concept clearly in an easy-to-grasp manner: ",
|
20 |
+
"Busy Professionals": "Summarize this concept briefly and concisely, focusing only on the essential details to save time, while keeping it professional and clear: ",
|
21 |
+
"Curious Learners": "Explain this concept in detail, diving into its meaning, examples, and practical relevance, while maintaining clarity and flow: ",
|
22 |
+
"Tech Enthusiasts": "Provide an insightful and technical explanation of this concept, including its relevance, practical applications, and deeper implications in the tech world: ",
|
23 |
+
"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: ",
|
24 |
+
"Business Leaders": "Explain this concept from a strategic perspective, focusing on its business relevance, use cases, and real-world value in a professional setting: ",
|
25 |
+
"Problem Solvers": "Describe this concept with a problem-solving mindset, focusing on practical applications, benefits, and how it can be applied to resolve challenges: "
|
26 |
+
}
|
27 |
+
|
28 |
+
|
29 |
+
# Prepend the category-specific prompt
|
30 |
+
category_prompt = category_prompts.get(category, "")
|
31 |
+
full_prompt = category_prompt + prompt
|
32 |
+
|
33 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
34 |
+
outputs = model.generate(
|
35 |
+
**inputs,
|
36 |
+
max_length=max_length,
|
37 |
+
temperature=temperature,
|
38 |
+
do_sample=True
|
39 |
+
)
|
40 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
print(generated_text)
|
42 |
+
return generated_text
|
43 |
+
|
44 |
+
# Gradio app interface with input and output components
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("#Tech Explainer\nEnter a concept, select a category, and Falcon 3-7B-Instruct will generate a simplified explanation!")
|
47 |
+
with gr.Row():
|
48 |
+
prompt_input = gr.Textbox(label="Enter your concept here", lines=3, placeholder="Type something...")
|
49 |
+
with gr.Row():
|
50 |
+
category_input = gr.Dropdown([
|
51 |
+
"Elder-Friendly", "Kid-Friendly", "Teen-Friendly",
|
52 |
+
"Beginner Coders", "Non-Techies", "Visual Thinkers",
|
53 |
+
"Busy Professionals", "Curious Learners",
|
54 |
+
"Tech Enthusiasts", "Educators",
|
55 |
+
"Business Leaders", "Problem Solvers"
|
56 |
+
], label="Select Audience Category", value="Elder-Friendly")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
max_length = gr.Slider(50, 500, value=150, step=10, label="Max Length")
|
60 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
61 |
+
with gr.Row():
|
62 |
+
generate_button = gr.Button("Generate Explanation")
|
63 |
+
with gr.Row():
|
64 |
+
output = gr.Textbox(label="Generated Explanation", lines=10)
|
65 |
+
|
66 |
+
generate_button.click(generate_text, inputs=[prompt_input, max_length, temperature, category_input], outputs=output)
|
67 |
+
|
68 |
+
demo.launch()
|