Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Install required packages
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import requests
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 10 |
+
|
| 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 Free MasterClass",
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
gradio
|
| 3 |
+
groq
|