Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import langdetect as detect
|
4 |
+
|
5 |
+
|
6 |
+
# Set up OpenAI API with custom Groq endpoint
|
7 |
+
openai.api_key = "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"
|
8 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
9 |
+
|
10 |
+
# Function to get the Groq model's response
|
11 |
+
def get_groq_response(message, mode):
|
12 |
+
try:
|
13 |
+
# Use a system prompt tailored to the selected mode
|
14 |
+
motivational_message = (
|
15 |
+
"Keep pushing forward! You've got this. Programming might seem tough at first, but every step you take "
|
16 |
+
"is one step closer to mastering it. Let's score 70 on 70 in programming together!"
|
17 |
+
)
|
18 |
+
|
19 |
+
if mode == "Code":
|
20 |
+
system_prompt = (
|
21 |
+
"You are GS C PrepBuddy, a friendly and motivational AI specializing in C programming. "
|
22 |
+
"Your goal is to assist users by providing clear, concise, and well-commented C code "
|
23 |
+
"to solve their problems. Explain the logic where necessary. "
|
24 |
+
+ motivational_message
|
25 |
+
)
|
26 |
+
elif mode == "Flowchart":
|
27 |
+
system_prompt = (
|
28 |
+
"You are GS C PrepBuddy, a friendly and motivational AI specializing in C programming. "
|
29 |
+
"Provide a detailed textual description of flowcharts to represent solutions for C programming problems. "
|
30 |
+
+ motivational_message
|
31 |
+
)
|
32 |
+
elif mode == "Algorithm":
|
33 |
+
system_prompt = (
|
34 |
+
"You are GS C PrepBuddy, a friendly and motivational AI specializing in C programming. "
|
35 |
+
"Provide step-by-step algorithms to solve the user's query with clarity. "
|
36 |
+
+ motivational_message
|
37 |
+
)
|
38 |
+
elif mode == "Exam Preparation":
|
39 |
+
system_prompt = (
|
40 |
+
"You are GS C PrepBuddy, a friendly and motivational AI dedicated to helping students prepare for their exams. "
|
41 |
+
"Provide explanations, theoretical concepts, sample questions, and problem-solving techniques in C programming. "
|
42 |
+
+ motivational_message
|
43 |
+
)
|
44 |
+
else:
|
45 |
+
system_prompt = "You are GS C PrepBuddy, a helpful and motivational AI assistant."
|
46 |
+
|
47 |
+
response = openai.ChatCompletion.create(
|
48 |
+
model="llama-3.1-70b-versatile",
|
49 |
+
messages=[
|
50 |
+
{"role": "system", "content": system_prompt},
|
51 |
+
{"role": "user", "content": message}
|
52 |
+
]
|
53 |
+
)
|
54 |
+
return response.choices[0].message["content"]
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error: {str(e)}"
|
57 |
+
|
58 |
+
# Function to handle chatbot interactions
|
59 |
+
def chatbot(user_input, output_format, history=[]):
|
60 |
+
try:
|
61 |
+
# Get the response based on the selected output format
|
62 |
+
bot_response = get_groq_response(user_input, output_format)
|
63 |
+
|
64 |
+
# Append to conversation history
|
65 |
+
history.append((user_input, bot_response))
|
66 |
+
return history, history # Return updated chat history and state
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
return [(user_input, f"Error: {str(e)}")], history
|
70 |
+
|
71 |
+
# Gradio Interface setup
|
72 |
+
chat_interface = gr.Interface(
|
73 |
+
fn=chatbot, # Function to call for chatbot interaction
|
74 |
+
inputs=[
|
75 |
+
"text", # User input
|
76 |
+
gr.Dropdown(
|
77 |
+
["Code", "Flowchart", "Algorithm", "Exam Preparation"],
|
78 |
+
label="Output Format",
|
79 |
+
value="Code"
|
80 |
+
),
|
81 |
+
"state" # Chat history
|
82 |
+
],
|
83 |
+
outputs=["chatbot", "state"], # Outputs: the chat and updated history (state)
|
84 |
+
live=False, # Disable live chat, responses shown after submit
|
85 |
+
title="GS C PrepBuddy", # Title of the app
|
86 |
+
description=(
|
87 |
+
"Welcome to GS Programming PrepBuddy! 💻✨\n\n"
|
88 |
+
"Let's make programming fun and score 70 on 70 in your C programming exams!\n\n"
|
89 |
+
"Choose your output format—Code, Flowchart, Algorithm, or Exam Preparation.\n\n"
|
90 |
+
"Ask your query, and I'll guide you every step of the way!\n\n"
|
91 |
+
"Made by Satyam Singhal"
|
92 |
+
)
|
93 |
+
)
|
94 |
+
|
95 |
+
# Launch the Gradio interface
|
96 |
+
if __name__ == "__main__":
|
97 |
+
chat_interface.launch()
|
98 |
+
|