File size: 2,176 Bytes
9b00cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv

# Load API Key from .env
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")

# Initialize OpenAI client for Groq
client = OpenAI(
    base_url="https://api.groq.com/openai/v1",
    api_key=api_key
)

# Function to generate response
def generate_response(mode, user_input):
    if not user_input.strip():
        return "⚠️ Please enter a topic or question."

    if mode == "Quiz":
        prompt = (
            f"Create a multiple choice quiz question on this topic: {user_input}.\n"
            "Include 4 answer options labeled A to D, and mention the correct answer."
        )
    elif mode == "Story":
        prompt = f"Write a fun and short story for kids about: {user_input}."
    elif mode == "Math":
        prompt = f"Solve this math problem with detailed steps: {user_input}."
    else:
        return "⚠️ Invalid mode selected."

    try:
        chat_completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7,
            max_tokens=300
        )
        return chat_completion.choices[0].message.content.strip()
    except Exception as e:
        return f"❌ Error: {str(e)}"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🧠 Mini Prompt App (Powered by Groq + LLaMA 3.3)")
    gr.Markdown("Choose a mode and enter a topic or question below:")

    mode = gr.Radio(choices=["Quiz", "Story", "Math"], label="Prompt Type", value="Story")
    user_input = gr.Textbox(label="Your Topic or Question", placeholder="E.g. A robot who loves to sing")

    output = gr.Textbox(label="Model Output", lines=10)

    submit_btn = gr.Button("Generate Response")
    submit_btn.click(generate_response, inputs=[mode, user_input], outputs=output)

    gr.Markdown("""

    ### 💡 Example Inputs:

    - Story: "A cat who goes to school"

    - Quiz: "Newton's laws of motion"

    - Math: "Simplify: (3x + 2)(x - 4)"

    """)

# Launch
if __name__ == "__main__":
    demo.launch()