Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,130 +3,86 @@ from groq import Groq
|
|
| 3 |
import gradio as gr
|
| 4 |
import logging
|
| 5 |
|
| 6 |
-
os.environ["GROQ_API_KEY"] = "
|
| 7 |
|
| 8 |
-
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 9 |
-
|
| 10 |
-
# Set up logging configuration
|
| 11 |
logging.basicConfig(level=logging.DEBUG)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# Initialize the Groq client
|
| 15 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 16 |
if not api_key:
|
| 17 |
logger.error("GROQ_API_KEY environment variable is not set.")
|
| 18 |
raise ValueError("GROQ_API_KEY environment variable is required.")
|
| 19 |
client = Groq(api_key=api_key)
|
| 20 |
|
| 21 |
-
# Default model name, can be overridden by environment variable
|
| 22 |
MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
|
| 23 |
|
|
|
|
| 24 |
def get_completion(user_input):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
str: The generated response or an error message.
|
| 33 |
-
"""
|
| 34 |
try:
|
| 35 |
completion = client.chat.completions.create(
|
| 36 |
model=MODEL_NAME,
|
| 37 |
messages=[
|
| 38 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 39 |
{"role": "user", "content": user_input}
|
| 40 |
],
|
| 41 |
-
temperature=
|
| 42 |
max_tokens=1024,
|
| 43 |
top_p=1,
|
| 44 |
-
stream=
|
|
|
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
logger.error(f"Error during completion: {e}")
|
| 52 |
return "Sorry, I encountered an error while processing your request."
|
| 53 |
|
|
|
|
| 54 |
def launch_interface():
|
| 55 |
-
"""
|
| 56 |
-
Launch the Gradio interface for the chatbot.
|
| 57 |
-
"""
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=get_completion,
|
| 60 |
inputs=gr.Textbox(
|
| 61 |
-
label="
|
| 62 |
-
placeholder="
|
| 63 |
lines=2,
|
| 64 |
-
max_lines=5
|
|
|
|
|
|
|
| 65 |
),
|
| 66 |
outputs=gr.Textbox(
|
| 67 |
label="Response:",
|
|
|
|
|
|
|
| 68 |
lines=6,
|
| 69 |
max_lines=10
|
| 70 |
),
|
| 71 |
-
title="Mr AI",
|
| 72 |
-
description=""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
border-radius:
|
| 76 |
-
|
| 77 |
-
padding:
|
| 78 |
-
background-color: #
|
| 79 |
-
}
|
| 80 |
-
.gr-input {
|
| 81 |
-
font-size: 1.2em;
|
| 82 |
-
padding: 10px;
|
| 83 |
-
border-radius: 5px;
|
| 84 |
-
border: 2px solid #007BFF;
|
| 85 |
-
margin-bottom: 10px;
|
| 86 |
-
}
|
| 87 |
-
.gr-output {
|
| 88 |
-
font-size: 1.2em;
|
| 89 |
-
padding: 10px;
|
| 90 |
-
border-radius: 5px;
|
| 91 |
-
border: 2px solid #28A745;
|
| 92 |
-
background-color: #E9F7EF;
|
| 93 |
-
}
|
| 94 |
-
.gr-interface-title {
|
| 95 |
-
font-size: 2em;
|
| 96 |
-
font-weight: bold;
|
| 97 |
-
color: #007BFF;
|
| 98 |
-
text-align: center;
|
| 99 |
-
margin-bottom: 20px;
|
| 100 |
-
}
|
| 101 |
-
.gr-interface-description {
|
| 102 |
-
font-size: 1.2em;
|
| 103 |
-
color: #6C757D;
|
| 104 |
-
text-align: center;
|
| 105 |
-
margin-bottom: 20px;
|
| 106 |
-
}
|
| 107 |
-
.gr-button {
|
| 108 |
-
background-color: #007BFF;
|
| 109 |
-
color: white;
|
| 110 |
-
border-radius: 5px;
|
| 111 |
-
padding: 10px 20px;
|
| 112 |
-
font-size: 1em;
|
| 113 |
-
border: none;
|
| 114 |
-
cursor: pointer;
|
| 115 |
-
margin-top: 10px;
|
| 116 |
-
}
|
| 117 |
-
.gr-button:hover {
|
| 118 |
-
background-color: #0056b3;
|
| 119 |
-
}
|
| 120 |
-
</style>
|
| 121 |
-
<div class="gr-interface-title">Welcome to Mr AI</div>
|
| 122 |
-
<div class="gr-interface-description">Ask anything and get a helpful response.</div>
|
| 123 |
""",
|
| 124 |
allow_flagging="never",
|
| 125 |
-
live=True
|
| 126 |
)
|
| 127 |
-
|
| 128 |
logger.info("Starting Gradio interface")
|
| 129 |
demo.launch(share=True)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
-
launch_interface()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import logging
|
| 5 |
|
| 6 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
+
# Initialize the Groq client
|
| 12 |
api_key = os.environ.get("GROQ_API_KEY")
|
| 13 |
if not api_key:
|
| 14 |
logger.error("GROQ_API_KEY environment variable is not set.")
|
| 15 |
raise ValueError("GROQ_API_KEY environment variable is required.")
|
| 16 |
client = Groq(api_key=api_key)
|
| 17 |
|
|
|
|
| 18 |
MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
|
| 19 |
|
| 20 |
+
# Define a function to handle chat completions
|
| 21 |
def get_completion(user_input):
|
| 22 |
+
if not user_input.strip():
|
| 23 |
+
return "Please enter a valid query."
|
| 24 |
+
|
| 25 |
+
# Check if the user asks "Who made you?"
|
| 26 |
+
if "who made you" in user_input.lower():
|
| 27 |
+
return "I was created by Thirumoorthi, a brilliant mind working on AI systems!"
|
| 28 |
+
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
completion = client.chat.completions.create(
|
| 31 |
model=MODEL_NAME,
|
| 32 |
messages=[
|
| 33 |
+
{"role": "system", "content": "You are a friendly and helpful assistant, like ChatGPT."},
|
| 34 |
{"role": "user", "content": user_input}
|
| 35 |
],
|
| 36 |
+
temperature=0.7, # Slightly lower temperature for more controlled responses
|
| 37 |
max_tokens=1024,
|
| 38 |
top_p=1,
|
| 39 |
+
stream=True,
|
| 40 |
+
stop=None,
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
response = ""
|
| 44 |
+
for chunk in completion:
|
| 45 |
+
response += chunk.choices[0].delta.content or ""
|
| 46 |
+
|
| 47 |
+
return response.strip() # Clean up response
|
| 48 |
except Exception as e:
|
| 49 |
logger.error(f"Error during completion: {e}")
|
| 50 |
return "Sorry, I encountered an error while processing your request."
|
| 51 |
|
| 52 |
+
# Launch Gradio interface
|
| 53 |
def launch_interface():
|
|
|
|
|
|
|
|
|
|
| 54 |
demo = gr.Interface(
|
| 55 |
fn=get_completion,
|
| 56 |
inputs=gr.Textbox(
|
| 57 |
+
label="Ask me anything:",
|
| 58 |
+
placeholder="I am here to help! Ask away...",
|
| 59 |
lines=2,
|
| 60 |
+
max_lines=5,
|
| 61 |
+
show_label=True,
|
| 62 |
+
interactive=True
|
| 63 |
),
|
| 64 |
outputs=gr.Textbox(
|
| 65 |
label="Response:",
|
| 66 |
+
interactive=False,
|
| 67 |
+
show_label=True,
|
| 68 |
lines=6,
|
| 69 |
max_lines=10
|
| 70 |
),
|
| 71 |
+
title="Chat with Mr AI",
|
| 72 |
+
description="I am your friendly assistant, just like ChatGPT! Ask me anything, and I will do my best to help.",
|
| 73 |
+
theme="huggingface", # More modern theme
|
| 74 |
+
css="""
|
| 75 |
+
.gr-box { border-radius: 15px; border: 1px solid #e1e1e1; padding: 20px; background-color: #f9f9f9; }
|
| 76 |
+
.gr-button { background-color: #4CAF50; color: white; font-size: 14px; }
|
| 77 |
+
.gr-textbox { border-radius: 8px; font-size: 16px; padding: 10px; }
|
| 78 |
+
.gr-output { background-color: #f1f1f1; border-radius: 8px; font-size: 16px; padding: 15px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
""",
|
| 80 |
allow_flagging="never",
|
| 81 |
+
live=True, # Enable live updates if supported
|
| 82 |
)
|
| 83 |
+
|
| 84 |
logger.info("Starting Gradio interface")
|
| 85 |
demo.launch(share=True)
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
launch_interface()
|