Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
# 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: | |
messages = [ | |
{"role": "system", "content": f""" | |
You are a professional astrologer with mystical wisdom. Analyze the birth chart for {name} born on {dob} | |
at {time_of_birth} in {place_of_birth}. Consider their {zodiac_sign} sun sign and current planetary | |
transits. Provide detailed, personalized insights in a mystical yet clear tone. Include: | |
- Key planetary aspects | |
- Strengths and challenges | |
- Career and relationship guidance | |
- Personalized recommendations | |
"""}, | |
{"role": "user", "content": query} | |
] | |
response = openai.ChatCompletion.create( | |
model="llama3-70b-8192", # Corrected Groq model name | |
messages=messages, | |
temperature=0.7, | |
max_tokens=500 | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"๐ฎ An error occurred: {str(e)} Please try again." | |
def chatbot(name, dob, time_of_birth, place_of_birth, zodiac_sign, query, chat_history): | |
# Initialize chat history if None | |
if chat_history is None: | |
chat_history = [] | |
# Get AI response | |
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query) | |
# Update chat history | |
chat_history.append((f"{query}", f"{bot_response}")) | |
return chat_history, chat_history | |
# Create Gradio components | |
with gr.Blocks(theme=gr.themes.Soft(), title="MysticAI Astrologer ๐ฎ") as demo: | |
gr.Markdown("# ๐ MysticAI Astrologer") | |
gr.Markdown("Discover your cosmic blueprint with AI-powered astrology insights") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
name = gr.Textbox(label="Full Name") | |
dob = gr.Textbox(label="Date of Birth (DD-MM-YYYY)") | |
time_of_birth = gr.Textbox(label="Exact Birth Time (HH:MM AM/PM)") | |
place_of_birth = gr.Textbox(label="Birth Place (City, Country)") | |
zodiac = gr.Dropdown( | |
choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", | |
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"], | |
label="Sun Sign" | |
) | |
with gr.Column(scale=2): | |
chatbot = gr.Chatbot(height=500) | |
query = gr.Textbox(label="Your Astrological Question", placeholder="Ask about your career, relationships, or upcoming transits...") | |
state = gr.State() | |
submit_btn = gr.Button("Consult the Stars โจ", variant="primary") | |
submit_btn.click( | |
chatbot, | |
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state], | |
outputs=[chatbot, state] | |
) | |
if __name__ == "__main__": | |
demo.launch() |