patmakur commited on
Commit
3689902
·
verified ·
1 Parent(s): 31eba7d

Create app.py

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