File size: 4,497 Bytes
f010434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad72b3e
f010434
 
8dc1b2f
f010434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec09a79
f010434
 
 
 
9cd74ba
 
ec09a79
5cdf426
 
 
 
 
 
c182a66
f010434
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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()