Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
from dotenv import load_dotenv | |
# Load environment variables | |
load_dotenv() | |
# Configure OpenAI API for Groq | |
openai.api_key = os.getenv("API_KEY") | |
openai.api_base = "https://api.groq.com/openai/v1" | |
def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query): | |
try: | |
if not openai.api_key: | |
return "๐ด Error: API key not found. Please check your configuration." | |
system_prompt = f"""You are Master Celestia, an AI astrologer with 500 years of mystical wisdom. Analyze the birth chart for: | |
Name: {name} | |
DOB: {dob} | |
Time: {time_of_birth} | |
Place: {place_of_birth} | |
Zodiac: {zodiac_sign} | |
Provide a professional analysis including: | |
1. Current planetary transits and aspects | |
2. Career and financial outlook (next 3 months) | |
3. Relationship compatibility insights | |
4. Personalized recommendations (crystals, mantras, colors) | |
5. Weekly guidance based on moon phases | |
Format with emojis and section headers. Keep responses under 600 tokens.""" | |
messages = [ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": query} | |
] | |
response = openai.ChatCompletion.create( | |
model="llama3-70b-8192", | |
messages=messages, | |
temperature=0.75, | |
max_tokens=600, | |
top_p=0.9 | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"๐ด Cosmic Interference! Please try again. Error: {str(e)}" | |
def handle_chat(name, dob, time_of_birth, place_of_birth, zodiac, query, chat_history): | |
# Validate required fields | |
if not all([name.strip(), dob.strip(), query.strip()]): | |
return chat_history, "โ ๏ธ Please fill all required fields (Name, DOB, and Question)" | |
# Initialize chat history | |
chat_history = chat_history or [] | |
# Add user message | |
chat_history.append((query, None)) | |
# Get AI response | |
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac, query) | |
# Update chat history | |
chat_history[-1] = (query, bot_response) | |
return chat_history, chat_history | |
# Gradio UI | |
with gr.Blocks( | |
theme=gr.themes.Soft( | |
primary_hue="purple", | |
secondary_hue="pink" | |
), | |
title="Celestial Guide ๐", | |
css=".gradio-container {background: url('https://example.com/space-bg.jpg')}" | |
) as demo: | |
gr.Markdown(""" | |
# ๐ Celestial Guide - AI Astrology Companion | |
*Your personal cosmic advisor powered by Groq & Llama-3* | |
""") | |
with gr.Row(): | |
with gr.Column(scale=1, min_width=300): | |
gr.Markdown("## ๐ Birth Details") | |
name = gr.Textbox(label="Full Name", placeholder="Enter your full name...") | |
dob = gr.Textbox(label="Date of Birth (DD-MM-YYYY)", placeholder="e.g., 15-04-1990") | |
time_of_birth = gr.Textbox(label="Birth Time (HH:MM AM/PM)", placeholder="e.g., 08:45 PM") | |
place_of_birth = gr.Textbox(label="Birth Place", placeholder="City, Country") | |
zodiac = gr.Dropdown( | |
label="Sun Sign", | |
choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", | |
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"], | |
value="Aries" | |
) | |
with gr.Column(scale=2): | |
gr.Markdown("## ๐ฌ Cosmic Consultation") | |
chatbot = gr.Chatbot( | |
height=600, | |
label="Astrology Chat", | |
avatar_images=("๐ค", "๐ฎ"), | |
bubble_full_width=False | |
) | |
query = gr.Textbox( | |
label="Your Cosmic Question", | |
placeholder="Ask about love, career, or spiritual growth...", | |
lines=3 | |
) | |
with gr.Row(): | |
submit_btn = gr.Button("Consult the Stars ๐ ", variant="primary") | |
clear_btn = gr.Button("New Session โป๏ธ") | |
state = gr.State() | |
# Event handlers | |
submit_btn.click( | |
handle_chat, | |
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state], | |
outputs=[chatbot, state] | |
) | |
query.submit( | |
handle_chat, | |
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state], | |
outputs=[chatbot, state] | |
) | |
clear_btn.click(lambda: (None, None, None, None, None, None, []), | |
outputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, chatbot]) | |
if __name__ == "__main__": | |
demo.launch(share=False) |