Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import libraries
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
from groq import Groq
|
5 |
+
|
6 |
+
# Set up Groq API client (ensure GROQ_API_KEY is set in your environment or as a Hugging Face secret for deployment)
|
7 |
+
client = Groq(api_key="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")
|
8 |
+
|
9 |
+
|
10 |
+
# Function to interact with the LLM using Groq's API
|
11 |
+
def chatbot(user_input):
|
12 |
+
try:
|
13 |
+
# Send user input to the LLM and get a response
|
14 |
+
chat_completion = client.chat.completions.create(
|
15 |
+
messages=[
|
16 |
+
{
|
17 |
+
"role": "user",
|
18 |
+
"content": user_input,
|
19 |
+
}
|
20 |
+
],
|
21 |
+
model="llama3-8b-8192", # Replace with the desired model
|
22 |
+
)
|
23 |
+
response = chat_completion.choices[0].message.content
|
24 |
+
return response
|
25 |
+
except Exception as e:
|
26 |
+
# Handle errors gracefully
|
27 |
+
return f"An error occurred: {str(e)}"
|
28 |
+
|
29 |
+
# Create a Gradio interface for real-time interaction
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=chatbot,
|
32 |
+
inputs=gr.Textbox(label="Your Message", placeholder="Type a message here..."),
|
33 |
+
outputs=gr.Textbox(label="Response"),
|
34 |
+
title="Real-Time Text-to-Text Chatbot - by ATIF MEHMOOD",
|
35 |
+
description="You can Chat with the LLM in real-time using Groq's API.",
|
36 |
+
)
|
37 |
+
|
38 |
+
# Launch the Gradio app
|
39 |
+
iface.launch()
|