decodingdatascience commited on
Commit
6c59e66
·
verified ·
1 Parent(s): 8e83ea5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -1,29 +1,22 @@
1
- # Install required packages
2
-
3
-
4
  import os
5
  import requests
6
  import gradio as gr
7
 
8
- from google.colab import userdata # Secure storage for API keys in Colab
 
9
 
10
- groq_api_key = userdata.get("GROQ_API_KEY")
 
11
 
12
- # Define the URL for the Groq API endpoint
13
  url = "https://api.groq.com/openai/v1/chat/completions"
14
-
15
- # Set the headers for the API request
16
- headers = {
17
- "Authorization": f"Bearer {groq_api_key}"
18
- }
19
 
20
  # Function to interact with Groq API
21
  def chat_with_groq(user_input):
22
  body = {
23
  "model": "llama-3.1-8b-instant",
24
- "messages": [
25
- {"role": "user", "content": user_input}
26
- ]
27
  }
28
 
29
  response = requests.post(url, headers=headers, json=body)
@@ -38,9 +31,10 @@ interface = gr.Interface(
38
  fn=chat_with_groq,
39
  inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
40
  outputs=gr.Textbox(),
41
- title="DDS Chat with Groq AI (Llama 3.1-8B)",
42
  description="Type your question below and get a response powered by Groq's Llama 3.1-8B model."
43
  )
44
 
45
  # Launch Gradio app
46
- interface.launch()
 
 
 
 
 
1
  import os
2
  import requests
3
  import gradio as gr
4
 
5
+ # Retrieve the API key from the environment variable
6
+ groq_api_key = os.getenv("GROQ_API_KEY")
7
 
8
+ if not groq_api_key:
9
+ raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.")
10
 
11
+ # Define the API endpoint and headers
12
  url = "https://api.groq.com/openai/v1/chat/completions"
13
+ headers = {"Authorization": f"Bearer {groq_api_key}"}
 
 
 
 
14
 
15
  # Function to interact with Groq API
16
  def chat_with_groq(user_input):
17
  body = {
18
  "model": "llama-3.1-8b-instant",
19
+ "messages": [{"role": "user", "content": user_input}]
 
 
20
  }
21
 
22
  response = requests.post(url, headers=headers, json=body)
 
31
  fn=chat_with_groq,
32
  inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
33
  outputs=gr.Textbox(),
34
+ title="Chat with Groq AI (Llama 3.1-8B)",
35
  description="Type your question below and get a response powered by Groq's Llama 3.1-8B model."
36
  )
37
 
38
  # Launch Gradio app
39
+ if __name__ == "__main__":
40
+ interface.launch()