ChatBot / app.py
khababakhtar's picture
Update app.py
3417ec7 verified
import os
import groq
import gradio as gr
import sys
# Manually set the API key here
api_key = "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" # Replace with your actual API key
# Check if the API key is available
if not api_key:
print("❌ Error: GROQ_API_KEY is not set. Please add it manually in the script.")
sys.exit(1)
# Initialize the Groq client with the API key
client = groq.Client(api_key=api_key)
# Chatbot function using Groq API
def chat_with_groq(user_input, history):
messages = [{"role": "system", "content": "You are an AI assistant on a space exploration mission. Your role is to assist astronauts with tasks, provide critical information about the space mission, and offer detailed insights into planets, stars, spacecraft, and other aspects of space exploration."}]
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": user_input})
try:
response = client.chat.completions.create(
model="llama3-8b-8192",
messages=messages,
temperature=0.7
)
bot_reply = response.choices[0].message.content
history.append((user_input, bot_reply))
return history, bot_reply
except Exception as e:
print(f"Error during API call: {e}")
return history, "Error occurred during the chat. Please check the API key."
# Custom CSS for galaxy background in output window
custom_css = """
body {
background: url('https://wallpaperaccess.com/full/2055391.jpg') no-repeat center center fixed;
background-size: cover;
color: white;
font-family: 'Arial', sans-serif;
}
#chatbot-container {
background: rgba(0, 0, 0, 0.85);
border-radius: 12px;
padding: 20px;
box-shadow: 0 0 15px rgba(255, 255, 255, 0.3);
}
/* Style for chatbot messages (both user and bot) */
.gr-chatbot {
background: url('https://wallpaperaccess.com/full/2055391.jpg') no-repeat center center fixed !important;
background-size: cover !important;
border-radius: 12px;
color: white;
font-size: 16px;
text-shadow: 0px 0px 5px rgba(255, 255, 255, 0.8);
}
/* Title Styling */
h1 {
text-align: center;
color: #FFD700;
font-size: 36px;
text-shadow: 0px 0px 10px rgba(255, 255, 255, 0.8);
}
"""
# Create Gradio UI with a stunning space background
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("<h1 Engineering AI ChatBot πŸͺ</h1>")
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(elem_id="chatbot-container")
user_input = gr.Textbox(label="Type your message...", placeholder="Ask me anything about space! πŸš€")
state = gr.State([])
def respond(input_text, chat_history):
chat_history, bot_reply = chat_with_groq(input_text, chat_history)
return chat_history, bot_reply
user_input.submit(respond, [user_input, state], [chatbot, user_input])
# Launch Gradio app
demo.launch()