Wosqa commited on
Commit
8a025d6
·
verified ·
1 Parent(s): 5d4b003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -44
app.py CHANGED
@@ -1,54 +1,32 @@
1
- import gradio as gr
2
  import os
3
  import requests
 
4
 
5
- # API setup
6
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
7
- GROQ_API_URL = "https://api.groq.com/v1/chat/completions" # Latest URL
8
- MODEL_NAME = "llama3-8b-8192"
9
-
10
- SYSTEM_PROMPT = "You are a Programming Tutor. Explain programming concepts clearly and concisely."
11
-
12
- def respond(user_input, chat_history, temperature):
13
- # Convert Gradio history into list of dicts for GROQ
14
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
15
- for item in chat_history or []:
16
- if isinstance(item, tuple) and len(item) == 2:
17
- messages.append({"role": "user", "content": str(item[0])})
18
- messages.append({"role": "assistant", "content": str(item[1])})
19
- messages.append({"role": "user", "content": str(user_input)})
20
 
21
- # Payload
22
- payload = {
23
- "model": MODEL_NAME,
24
- "messages": messages,
25
- "temperature": float(temperature)
26
- }
27
-
28
- # POST request
29
  headers = {
30
- "Authorization": f"Bearer {GROQ_API_KEY}",
31
  "Content-Type": "application/json"
32
  }
33
- r = requests.post(GROQ_API_URL, headers=headers, json=payload)
34
-
35
- if r.status_code == 200:
36
- bot_reply = r.json()["choices"][0]["message"]["content"]
37
- else:
38
- bot_reply = f"Error {r.status_code}: {r.text}"
39
 
40
- new_history = (chat_history or []) + [(user_input, bot_reply)]
41
- return "", new_history
 
 
 
 
 
42
 
43
- # Gradio UI
44
- with gr.Blocks() as demo:
45
- gr.Markdown("## Programming Tutor Chatbot")
46
- chatbot = gr.Chatbot()
47
- state = gr.State([])
48
- msg = gr.Textbox(label="Ask a programming question")
49
- temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1)
50
- clear = gr.Button("Clear Chat")
51
- msg.submit(respond, [msg, state, temperature], [msg, chatbot])
52
- clear.click(lambda: ([], []), None, [chatbot, state])
53
 
54
- demo.launch()
 
 
 
 
1
  import os
2
  import requests
3
+ import gradio as gr
4
 
5
+ # Get API key from environment variable
6
+ API_KEY = os.environ.get("GROQ_API_KEY")
7
+ API_URL = "https://api.groq.com/openai/v1/chat/completions"
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Define a function to call the Groq API
10
+ def query_groq(prompt):
 
 
 
 
 
 
11
  headers = {
12
+ "Authorization": f"Bearer {API_KEY}",
13
  "Content-Type": "application/json"
14
  }
 
 
 
 
 
 
15
 
16
+ data = {
17
+ "model": "llama3-8b-8192",
18
+ "messages": [
19
+ {"role": "system", "content": "You are an expert Programming Tutor."},
20
+ {"role": "user", "content": prompt}
21
+ ]
22
+ }
23
 
24
+ response = requests.post(API_URL, headers=headers, json=data)
25
+ if response.status_code == 200:
26
+ return response.json()["choices"][0]["message"]["content"]
27
+ else:
28
+ return f"Error: {response.status_code} - {response.text}"
 
 
 
 
 
29
 
30
+ # Gradio interface
31
+ iface = gr.Interface(fn=query_groq, inputs="text", outputs="text", title="Groq Chat")
32
+ iface.launch()