Spaces:
Sleeping
Sleeping
Create app.py
#1
by
AThirumoorthi
- opened
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
import gradio as gr
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# Set up logging
|
7 |
+
logging.basicConfig(level=logging.DEBUG)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# Initialize the Groq client
|
11 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
12 |
+
if not api_key:
|
13 |
+
logger.error("GROQ_API_KEY environment variable is not set.")
|
14 |
+
raise ValueError("GROQ_API_KEY environment variable is required.")
|
15 |
+
client = Groq(api_key=api_key)
|
16 |
+
|
17 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
|
18 |
+
|
19 |
+
# Define a function to handle chat completions
|
20 |
+
def get_completion(user_input):
|
21 |
+
try:
|
22 |
+
completion = client.chat.completions.create(
|
23 |
+
model=MODEL_NAME,
|
24 |
+
messages=[
|
25 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
26 |
+
{"role": "user", "content": user_input}
|
27 |
+
],
|
28 |
+
temperature=1,
|
29 |
+
max_tokens=1024,
|
30 |
+
top_p=1,
|
31 |
+
stream=True,
|
32 |
+
stop=None,
|
33 |
+
)
|
34 |
+
|
35 |
+
response = ""
|
36 |
+
for chunk in completion:
|
37 |
+
response += chunk.choices[0].delta.content or ""
|
38 |
+
|
39 |
+
return response
|
40 |
+
except Exception as e:
|
41 |
+
logger.error(f"Error during completion: {e}")
|
42 |
+
return "Sorry, I encountered an error while processing your request."
|
43 |
+
|
44 |
+
# Launch Gradio interface
|
45 |
+
def launch_interface():
|
46 |
+
demo = gr.Interface(
|
47 |
+
fn=get_completion,
|
48 |
+
inputs=gr.Textbox(
|
49 |
+
label="Enter your query:",
|
50 |
+
placeholder="Ask me anything...",
|
51 |
+
lines=2,
|
52 |
+
max_lines=5,
|
53 |
+
show_label=True,
|
54 |
+
interactive=True
|
55 |
+
),
|
56 |
+
outputs=gr.Textbox(
|
57 |
+
label="Response:",
|
58 |
+
interactive=False,
|
59 |
+
show_label=True,
|
60 |
+
lines=6,
|
61 |
+
max_lines=10
|
62 |
+
),
|
63 |
+
title="Mr AI",
|
64 |
+
description="Ask anything and get a helpful response.",
|
65 |
+
theme="default",
|
66 |
+
css=".gr-box { border-radius: 10px; border: 1px solid #ccc; padding: 10px; }",
|
67 |
+
allow_flagging="never"
|
68 |
+
)
|
69 |
+
|
70 |
+
logger.info("Starting Gradio interface")
|
71 |
+
demo.launch(share=True)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
launch_interface()
|