Spaces:
Running
Running
import gradio as gr | |
import openai | |
# Configure OpenAI API | |
openai.api_key = getenv("GROQ_API_KEY") | |
openai.api_base = "https://api.groq.com/openai/v1" | |
# Function to get AI-generated astrology response | |
def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query): | |
try: | |
# Construct the message | |
messages = [ | |
{"role": "system", "content": """ | |
You are a mystical astrologer. Provide detailed insights about zodiac signs, planetary alignments, | |
and astrological predictions based on the user's inputs. Use a mystical and engaging tone. | |
"""}, | |
{"role": "user", "content": f""" | |
Name: {name} | |
Date of Birth: {dob} | |
Time of Birth: {time_of_birth} | |
Place of Birth: {place_of_birth} | |
Zodiac Sign: {zodiac_sign} | |
Query: {query} | |
"""} | |
] | |
# Generate response from the AI | |
response = openai.ChatCompletion.create( | |
model="llama-3.3-70b-versatile", | |
messages=messages | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Chatbot function to handle user input and history | |
def chatbot(name, dob, time_of_birth, place_of_birth, zodiac_sign, query, history=[]): | |
# Get the response from the AI | |
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query) | |
# Append the conversation to history | |
history.append((f"You: {query}", f"Astrologer: {bot_response}")) | |
return history, history | |
# Define Gradio interface | |
chat_interface = gr.Interface( | |
fn=chatbot, # Function to call for interaction | |
inputs=[ | |
gr.Textbox(label="Your Name"), # Name input | |
gr.Textbox(label="Date of Birth (e.g., 01-01-2000)"), # DOB input | |
gr.Textbox(label="Time of Birth (e.g., 10:30 AM)"), # Time of Birth input | |
gr.Textbox(label="Place of Birth"), # Place of Birth input | |
gr.Dropdown( | |
label="Your Zodiac Sign", | |
choices=[ | |
"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", | |
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces" | |
] | |
), # Dropdown for zodiac sign | |
gr.Textbox(label="Your Question for the Astrologer"), # Query input | |
gr.State(), # State for chat history | |
], | |
outputs=[ | |
gr.Chatbot(label="Astrology Chat"), # Chatbot interface for responses | |
gr.State(), # State to maintain chat history | |
], | |
title="Astrology AI Assistant π", | |
description=( | |
"Welcome to your personal Astrology AI Assistant! " | |
"Ask questions about your zodiac sign, horoscope, and planetary alignments. " | |
"Enter your details and let the stars guide you. β¨" | |
), | |
theme="dark" # Optional: Gradio theme customization | |
) | |
# Launch the Gradio app | |
if __name__ == "__main__": | |
chat_interface.launch() |