decodingdatascience commited on
Commit
5139312
·
verified ·
1 Parent(s): fc97065

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
30
+
31
+ if response.status_code == 200:
32
+ return response.json()['choices'][0]['message']['content']
33
+ else:
34
+ return f"Error: {response.json()}"
35
+
36
+ # Create Gradio interface
37
+ 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()