Aahi8828 commited on
Commit
a67595f
·
verified ·
1 Parent(s): 3a8c3c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,34 +1,50 @@
1
  import gradio as gr
2
  import openai
3
- import os
4
 
 
5
  openai.api_key = os.getenv("GROQ_API_KEY")
6
  openai.api_base = "https://api.groq.com/openai/v1"
7
 
8
  def get_groq_response(message):
9
- try:
10
- response = openai.ChatCompletion.create(
11
- model = "llama-3.3-70b-versatile",
12
- messages = [
13
- {"role": "system", "content": "Precise answer"},
14
- {"role": "user", "content": message}
15
- ]
16
- )
17
- return response.choices[0].message["content"]
18
- except Exception as e:
19
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- def chatbot(user_input, history = []):
22
- bot_response = get_groq_response(user_input)
23
- history.append((user_input, bot_response))
24
- return history, history
25
 
26
  chat_interface = gr.Interface(
27
  fn=chatbot,
28
- inputs= ["text", "state"],
29
  outputs=["chatbot", "state"],
30
  live=False,
31
  title="My Chatbot",
32
  description="Mom: We have ChatGPT at Home, \n ChatGPT at Home:"
33
  )
34
- chat_interface.launch()
 
 
1
  import gradio as gr
2
  import openai
3
+ import os
4
 
5
+ # Ensure the API key is loaded correctly
6
  openai.api_key = os.getenv("GROQ_API_KEY")
7
  openai.api_base = "https://api.groq.com/openai/v1"
8
 
9
  def get_groq_response(message):
10
+ try:
11
+ # Logging the message before sending
12
+ print(f"Sending message: {message}")
13
+
14
+ response = openai.ChatCompletion.create(
15
+ model="llama-3.3-70b-versatile",
16
+ messages=[
17
+ {"role": "system", "content": "Precise answer"},
18
+ {"role": "user", "content": message}
19
+ ]
20
+ )
21
+
22
+ # Correcting the response structure
23
+ bot_message = response['choices'][0]['message']['content']
24
+
25
+ # Log the bot's response
26
+ print(f"Received response: {bot_message}")
27
+
28
+ return bot_message
29
+
30
+ except Exception as e:
31
+ print(f"Error: {str(e)}")
32
+ return f"Error: {str(e)}"
33
+
34
+
35
+ def chatbot(user_input, history=[]):
36
+ bot_response = get_groq_response(user_input)
37
+ history.append((user_input, bot_response))
38
+ return history, history
39
 
 
 
 
 
40
 
41
  chat_interface = gr.Interface(
42
  fn=chatbot,
43
+ inputs=["text", "state"],
44
  outputs=["chatbot", "state"],
45
  live=False,
46
  title="My Chatbot",
47
  description="Mom: We have ChatGPT at Home, \n ChatGPT at Home:"
48
  )
49
+
50
+ chat_interface.launch()