Muhammad Ahmad Zia commited on
Upload 2 files
Browse files- app.py +79 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Load GROQ API key from environment
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
MODEL_NAME = "llama3-8b-8192"
|
| 9 |
+
|
| 10 |
+
def query_groq(message, chat_history, complexity):
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Customizing the prompt based on the UI improvement (Complexity Level)
|
| 17 |
+
system_message = f"""You are 'Sprint', an expert Unity Game Developer and C# Mentor.
|
| 18 |
+
Your goal is to help students build games.
|
| 19 |
+
Current Explanation Level: {complexity}.
|
| 20 |
+
If Beginner: Use simple analogies and explain basic terms.
|
| 21 |
+
If Intermediate: Focus on clean code and Unity best practices.
|
| 22 |
+
If Expert: Discuss optimization, design patterns, and low-level memory management.
|
| 23 |
+
Always be encouraging and provide code snippets in C# where applicable."""
|
| 24 |
+
|
| 25 |
+
messages = [{"role": "system", "content": system_message}]
|
| 26 |
+
|
| 27 |
+
for user, bot in chat_history:
|
| 28 |
+
messages.append({"role": "user", "content": user})
|
| 29 |
+
messages.append({"role": "assistant", "content": bot})
|
| 30 |
+
|
| 31 |
+
messages.append({"role": "user", "content": message})
|
| 32 |
+
|
| 33 |
+
payload = {
|
| 34 |
+
"model": MODEL_NAME,
|
| 35 |
+
"messages": messages,
|
| 36 |
+
"temperature": 0.7
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 43 |
+
return reply
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error: {str(e)}"
|
| 46 |
+
|
| 47 |
+
def respond(message, chat_history, complexity):
|
| 48 |
+
bot_reply = query_groq(message, chat_history, complexity)
|
| 49 |
+
chat_history.append((message, bot_reply))
|
| 50 |
+
return "", chat_history
|
| 51 |
+
|
| 52 |
+
# Gradio UI Layout
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
+
gr.Markdown("# 🎮 Sprint: Your Unity & C# Mentor")
|
| 55 |
+
gr.Markdown("I'm here to help you debug scripts, setup Colliders, or optimize your game performance.")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
# UI IMPROVEMENT: Complexity Level Selector
|
| 59 |
+
complexity_level = gr.Dropdown(
|
| 60 |
+
label="Explanation Complexity",
|
| 61 |
+
choices=["Beginner", "Intermediate", "Expert"],
|
| 62 |
+
value="Intermediate"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
chatbot = gr.Chatbot()
|
| 66 |
+
msg = gr.Textbox(label="Ask a Unity/C# question (e.g., 'How do I use Raycasts?')")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
submit = gr.Button("Submit", variant="primary")
|
| 70 |
+
clear = gr.Button("Clear Chat")
|
| 71 |
+
|
| 72 |
+
state = gr.State([])
|
| 73 |
+
|
| 74 |
+
# Event Handlers
|
| 75 |
+
msg.submit(respond, [msg, state, complexity_level], [msg, chatbot])
|
| 76 |
+
submit.click(respond, [msg, state, complexity_level], [msg, chatbot])
|
| 77 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 78 |
+
|
| 79 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|