Spaces:
Sleeping
Sleeping
File size: 3,098 Bytes
8879a78 047af61 e3fa415 047af61 512a676 dbb8943 047af61 e3fa415 8879a78 512a676 ca577b1 047af61 8879a78 047af61 8879a78 ca577b1 8879a78 047af61 e3fa415 ca577b1 e3fa415 047af61 cea41ae ca577b1 af934bd ca577b1 af934bd ca577b1 af934bd ca577b1 af934bd ca577b1 af934bd cea41ae af934bd ca577b1 af934bd ca577b1 3417ec7 ca577b1 047af61 8879a78 e3fa415 047af61 e3fa415 047af61 8879a78 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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()
|