Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,76 +2,76 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Configure OpenAI API
|
| 6 |
openai.api_key = os.getenv("API_KEY")
|
| 7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
|
| 9 |
-
# Function to get AI-generated astrology response
|
| 10 |
def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query):
|
| 11 |
try:
|
| 12 |
-
# Construct the message
|
| 13 |
messages = [
|
| 14 |
-
{"role": "system", "content": """
|
| 15 |
-
You are a
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""},
|
| 18 |
-
{"role": "user", "content":
|
| 19 |
-
Name: {name}
|
| 20 |
-
Date of Birth: {dob}
|
| 21 |
-
Time of Birth: {time_of_birth}
|
| 22 |
-
Place of Birth: {place_of_birth}
|
| 23 |
-
Zodiac Sign: {zodiac_sign}
|
| 24 |
-
Query: {query}
|
| 25 |
-
"""}
|
| 26 |
]
|
| 27 |
-
|
| 28 |
response = openai.ChatCompletion.create(
|
| 29 |
-
model="
|
| 30 |
-
messages=messages
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
return response.choices[0].message["content"]
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
label="
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
)
|
| 72 |
-
theme="dark" # Optional: Gradio theme customization
|
| 73 |
-
)
|
| 74 |
|
| 75 |
-
# Launch the Gradio app
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
|
|
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Configure OpenAI API for Groq
|
| 6 |
openai.api_key = os.getenv("API_KEY")
|
| 7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
|
|
|
|
| 9 |
def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query):
|
| 10 |
try:
|
|
|
|
| 11 |
messages = [
|
| 12 |
+
{"role": "system", "content": f"""
|
| 13 |
+
You are a professional astrologer with mystical wisdom. Analyze the birth chart for {name} born on {dob}
|
| 14 |
+
at {time_of_birth} in {place_of_birth}. Consider their {zodiac_sign} sun sign and current planetary
|
| 15 |
+
transits. Provide detailed, personalized insights in a mystical yet clear tone. Include:
|
| 16 |
+
- Key planetary aspects
|
| 17 |
+
- Strengths and challenges
|
| 18 |
+
- Career and relationship guidance
|
| 19 |
+
- Personalized recommendations
|
| 20 |
"""},
|
| 21 |
+
{"role": "user", "content": query}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
]
|
| 23 |
+
|
| 24 |
response = openai.ChatCompletion.create(
|
| 25 |
+
model="llama3-70b-8192", # Corrected Groq model name
|
| 26 |
+
messages=messages,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
max_tokens=500
|
| 29 |
)
|
| 30 |
return response.choices[0].message["content"]
|
| 31 |
except Exception as e:
|
| 32 |
+
return f"🔮 An error occurred: {str(e)} Please try again."
|
| 33 |
|
| 34 |
+
def chatbot(name, dob, time_of_birth, place_of_birth, zodiac_sign, query, chat_history):
|
| 35 |
+
# Initialize chat history if None
|
| 36 |
+
if chat_history is None:
|
| 37 |
+
chat_history = []
|
| 38 |
+
|
| 39 |
+
# Get AI response
|
| 40 |
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query)
|
| 41 |
+
|
| 42 |
+
# Update chat history
|
| 43 |
+
chat_history.append((f"{query}", f"{bot_response}"))
|
| 44 |
+
|
| 45 |
+
return chat_history, chat_history
|
| 46 |
|
| 47 |
+
# Create Gradio components
|
| 48 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="MysticAI Astrologer 🔮") as demo:
|
| 49 |
+
gr.Markdown("# 🌌 MysticAI Astrologer")
|
| 50 |
+
gr.Markdown("Discover your cosmic blueprint with AI-powered astrology insights")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column(scale=1):
|
| 54 |
+
name = gr.Textbox(label="Full Name")
|
| 55 |
+
dob = gr.Textbox(label="Date of Birth (DD-MM-YYYY)")
|
| 56 |
+
time_of_birth = gr.Textbox(label="Exact Birth Time (HH:MM AM/PM)")
|
| 57 |
+
place_of_birth = gr.Textbox(label="Birth Place (City, Country)")
|
| 58 |
+
zodiac = gr.Dropdown(
|
| 59 |
+
choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo",
|
| 60 |
+
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"],
|
| 61 |
+
label="Sun Sign"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Column(scale=2):
|
| 65 |
+
chatbot = gr.Chatbot(height=500)
|
| 66 |
+
query = gr.Textbox(label="Your Astrological Question", placeholder="Ask about your career, relationships, or upcoming transits...")
|
| 67 |
+
state = gr.State()
|
| 68 |
+
submit_btn = gr.Button("Consult the Stars ✨", variant="primary")
|
| 69 |
+
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
chatbot,
|
| 72 |
+
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
|
| 73 |
+
outputs=[chatbot, state]
|
| 74 |
+
)
|
|
|
|
|
|
|
| 75 |
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|