Spaces:
Sleeping
Sleeping
ae5115242430e13
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
import gradio as gr
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# Set up logging configuration
|
7 |
+
logging.basicConfig(level=logging.DEBUG)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# Initialize the Groq client with API key from environment variables
|
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 |
+
# Default model name, can be overridden by environment variable
|
18 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
|
19 |
+
|
20 |
+
def get_completion(user_input):
|
21 |
+
"""
|
22 |
+
Generate a chat completion response using the Groq client.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
user_input (str): The user's input query.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
str: The generated response or an error message.
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
completion = client.chat.completions.create(
|
32 |
+
model=MODEL_NAME,
|
33 |
+
messages=[
|
34 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
35 |
+
{"role": "user", "content": user_input}
|
36 |
+
],
|
37 |
+
temperature=1,
|
38 |
+
max_tokens=1024,
|
39 |
+
top_p=1,
|
40 |
+
stream=False, # Gradio does not support stream=True
|
41 |
+
)
|
42 |
+
|
43 |
+
# Extract the response
|
44 |
+
response = completion.choices[0].message.content.strip()
|
45 |
+
return response
|
46 |
+
except Exception as e:
|
47 |
+
logger.error(f"Error during completion: {e}")
|
48 |
+
return "Sorry, I encountered an error while processing your request."
|
49 |
+
|
50 |
+
def launch_interface():
|
51 |
+
"""
|
52 |
+
Launch the Gradio interface for the chatbot.
|
53 |
+
"""
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=get_completion,
|
56 |
+
inputs=gr.Textbox(
|
57 |
+
label="Enter your query:",
|
58 |
+
placeholder="Ask me anything...",
|
59 |
+
lines=2,
|
60 |
+
max_lines=5
|
61 |
+
),
|
62 |
+
outputs=gr.Textbox(
|
63 |
+
label="Response:",
|
64 |
+
lines=6,
|
65 |
+
max_lines=10
|
66 |
+
),
|
67 |
+
title="Mr AI",
|
68 |
+
description="""
|
69 |
+
<style>
|
70 |
+
.gr-box {
|
71 |
+
border-radius: 10px;
|
72 |
+
border: 2px solid #007BFF;
|
73 |
+
padding: 15px;
|
74 |
+
background-color: #F8F9FA;
|
75 |
+
}
|
76 |
+
.gr-input {
|
77 |
+
font-size: 1.2em;
|
78 |
+
padding: 10px;
|
79 |
+
border-radius: 5px;
|
80 |
+
border: 2px solid #007BFF;
|
81 |
+
margin-bottom: 10px;
|
82 |
+
}
|
83 |
+
.gr-output {
|
84 |
+
font-size: 1.2em;
|
85 |
+
padding: 10px;
|
86 |
+
border-radius: 5px;
|
87 |
+
border: 2px solid #28A745;
|
88 |
+
background-color: #E9F7EF;
|
89 |
+
}
|
90 |
+
.gr-interface-title {
|
91 |
+
font-size: 2em;
|
92 |
+
font-weight: bold;
|
93 |
+
color: #007BFF;
|
94 |
+
text-align: center;
|
95 |
+
margin-bottom: 20px;
|
96 |
+
}
|
97 |
+
.gr-interface-description {
|
98 |
+
font-size: 1.2em;
|
99 |
+
color: #6C757D;
|
100 |
+
text-align: center;
|
101 |
+
margin-bottom: 20px;
|
102 |
+
}
|
103 |
+
.gr-button {
|
104 |
+
background-color: #007BFF;
|
105 |
+
color: white;
|
106 |
+
border-radius: 5px;
|
107 |
+
padding: 10px 20px;
|
108 |
+
font-size: 1em;
|
109 |
+
border: none;
|
110 |
+
cursor: pointer;
|
111 |
+
margin-top: 10px;
|
112 |
+
}
|
113 |
+
.gr-button:hover {
|
114 |
+
background-color: #0056b3;
|
115 |
+
}
|
116 |
+
</style>
|
117 |
+
<div class="gr-interface-title">Welcome to Mr AI</div>
|
118 |
+
<div class="gr-interface-description">Ask anything and get a helpful response.</div>
|
119 |
+
""",
|
120 |
+
allow_flagging="never",
|
121 |
+
live=True
|
122 |
+
)
|
123 |
+
|
124 |
+
logger.info("Starting Gradio interface")
|
125 |
+
demo.launch(share=True)
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
launch_interface()
|