R.s.L / app.py
AThirumoorthi's picture
Update app.py
1a43d16 verified
raw
history blame
3.09 kB
import os
from groq import Groq
import gradio as gr
import logging
os.environ["GROQ_API_KEY"] = "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"
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Initialize the Groq client
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
logger.error("GROQ_API_KEY environment variable is not set.")
raise ValueError("GROQ_API_KEY environment variable is required.")
client = Groq(api_key=api_key)
MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
# Define a function to handle chat completions
def get_completion(user_input):
if not user_input.strip():
return "Please enter a valid query."
# Check if the user asks "Who made you?"
if "who made you" in user_input.lower():
return "I was created by Thirumoorthi, a brilliant mind working on AI systems!"
try:
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": "You are a friendly and helpful assistant, like ChatGPT."},
{"role": "user", "content": user_input}
],
temperature=0.7, # Slightly lower temperature for more controlled responses
max_tokens=1024,
top_p=1,
stream=True,
stop=None,
)
response = ""
for chunk in completion:
response += chunk.choices[0].delta.content or ""
return response.strip() # Clean up response
except Exception as e:
logger.error(f"Error during completion: {e}")
return "Sorry, I encountered an error while processing your request."
# Launch Gradio interface
def launch_interface():
demo = gr.Interface(
fn=get_completion,
inputs=gr.Textbox(
label="Ask me anything:",
placeholder="I am here to help! Ask away...",
lines=2,
max_lines=5,
show_label=True,
interactive=True
),
outputs=gr.Textbox(
label="Response:",
interactive=False,
show_label=True,
lines=6,
max_lines=10
),
title="Chat with Mr AI",
description="I am your friendly assistant, just like ChatGPT! Ask me anything, and I will do my best to help.",
theme="huggingface", # More modern theme
css="""
.gr-box { border-radius: 15px; border: 1px solid #e1e1e1; padding: 20px; background-color: #f9f9f9; }
.gr-button { background-color: #4CAF50; color: white; font-size: 14px; }
.gr-textbox { border-radius: 8px; font-size: 16px; padding: 10px; }
.gr-output { background-color: #f1f1f1; border-radius: 8px; font-size: 16px; padding: 15px; }
""",
allow_flagging="never",
live=True, # Enable live updates if supported
)
logger.info("Starting Gradio interface")
demo.launch(share=True)
if __name__ == "__main__":
launch_interface()