decodingdatascience commited on
Commit
d0fa01e
·
verified ·
1 Parent(s): 8fde0ee

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +65 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install requests
2
+ #pip install groq
3
+ #pip install python-dotenv
4
+ #pip install gradio
5
+ #add .env with GROQ api key in local
6
+ # use 3.11 python
7
+
8
+ import os
9
+ import requests
10
+ import gradio as gr
11
+ from dotenv import load_dotenv
12
+
13
+ # Load environment variables from .env file
14
+ load_dotenv()
15
+
16
+ # Get the Groq API key from environment variables
17
+ groq_api_key = os.environ.get("GROQ_API_KEY")
18
+
19
+ # Check if the Groq API key is set
20
+ if groq_api_key is None:
21
+ raise ValueError("Groq API key is not set in environment variables.")
22
+
23
+ # Define the URL for the Groq API endpoint
24
+ url = "https://api.groq.com/openai/v1/chat/completions"
25
+
26
+ # Function to interact with the Groq API
27
+ def groq_chat(prompt):
28
+ headers = {
29
+ "Authorization": f"Bearer {groq_api_key}"
30
+ }
31
+ body = {
32
+ "model": "llama-3.1-8b-instant",
33
+ "messages": [
34
+ {
35
+ "role": "user",
36
+ "content": prompt
37
+ }
38
+ ]
39
+ }
40
+
41
+ # Send a POST request to the Groq API
42
+ response = requests.post(url, headers=headers, json=body)
43
+
44
+ if response.status_code == 200:
45
+ # Extract and return the content of the first message choice
46
+ return response.json().get('choices', [{}])[0].get('message', {}).get('content', "No response found.")
47
+ else:
48
+ # Return the error details
49
+ return f"Error {response.status_code}: {response.text}"
50
+
51
+ # Define the Gradio interface
52
+ with gr.Blocks() as interface:
53
+ gr.Markdown("# DDS 1st Chatbot")
54
+ with gr.Row():
55
+ user_input = gr.Textbox(label="Enter your prompt", placeholder="Type something funny or interesting...")
56
+ with gr.Row():
57
+ output = gr.Textbox(label="Response from Groq API")
58
+ with gr.Row():
59
+ submit_button = gr.Button("Get Response")
60
+
61
+ submit_button.click(fn=groq_chat, inputs=user_input, outputs=output)
62
+
63
+ # Launch the interface
64
+ if __name__ == "__main__":
65
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ requests
2
+ groq
3
+ python-dotenv
4
+ gradio