Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Configure OpenAI API for Groq
|
6 |
openai.api_key = os.getenv("API_KEY")
|
@@ -8,70 +12,122 @@ 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":
|
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",
|
26 |
messages=messages,
|
27 |
-
temperature=0.
|
28 |
-
max_tokens=
|
|
|
29 |
)
|
30 |
return response.choices[0].message["content"]
|
31 |
except Exception as e:
|
32 |
-
return f"
|
33 |
|
34 |
-
def
|
35 |
-
#
|
36 |
-
if
|
37 |
-
chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Get AI response
|
40 |
-
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth,
|
41 |
|
42 |
# Update chat history
|
43 |
-
chat_history
|
44 |
|
45 |
return chat_history, chat_history
|
46 |
|
47 |
-
#
|
48 |
-
with gr.Blocks(
|
49 |
-
gr.
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
with gr.Row():
|
53 |
-
with gr.Column(scale=1):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
zodiac = gr.Dropdown(
|
|
|
59 |
choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo",
|
60 |
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"],
|
61 |
-
|
62 |
)
|
63 |
-
|
64 |
with gr.Column(scale=2):
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
70 |
submit_btn.click(
|
71 |
-
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
|
9 |
# Configure OpenAI API for Groq
|
10 |
openai.api_key = os.getenv("API_KEY")
|
|
|
12 |
|
13 |
def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, query):
|
14 |
try:
|
15 |
+
if not openai.api_key:
|
16 |
+
return "๐ด Error: API key not found. Please check your configuration."
|
17 |
+
|
18 |
+
system_prompt = f"""You are Master Celestia, an AI astrologer with 500 years of mystical wisdom. Analyze the birth chart for:
|
19 |
+
Name: {name}
|
20 |
+
DOB: {dob}
|
21 |
+
Time: {time_of_birth}
|
22 |
+
Place: {place_of_birth}
|
23 |
+
Zodiac: {zodiac_sign}
|
24 |
+
|
25 |
+
Provide a professional analysis including:
|
26 |
+
1. Current planetary transits and aspects
|
27 |
+
2. Career and financial outlook (next 3 months)
|
28 |
+
3. Relationship compatibility insights
|
29 |
+
4. Personalized recommendations (crystals, mantras, colors)
|
30 |
+
5. Weekly guidance based on moon phases
|
31 |
+
|
32 |
+
Format with emojis and section headers. Keep responses under 600 tokens."""
|
33 |
+
|
34 |
messages = [
|
35 |
+
{"role": "system", "content": system_prompt},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
{"role": "user", "content": query}
|
37 |
]
|
38 |
|
39 |
response = openai.ChatCompletion.create(
|
40 |
+
model="llama3-70b-8192",
|
41 |
messages=messages,
|
42 |
+
temperature=0.75,
|
43 |
+
max_tokens=600,
|
44 |
+
top_p=0.9
|
45 |
)
|
46 |
return response.choices[0].message["content"]
|
47 |
except Exception as e:
|
48 |
+
return f"๐ด Cosmic Interference! Please try again. Error: {str(e)}"
|
49 |
|
50 |
+
def handle_chat(name, dob, time_of_birth, place_of_birth, zodiac, query, chat_history):
|
51 |
+
# Validate required fields
|
52 |
+
if not all([name.strip(), dob.strip(), query.strip()]):
|
53 |
+
return chat_history, "โ ๏ธ Please fill all required fields (Name, DOB, and Question)"
|
54 |
+
|
55 |
+
# Initialize chat history
|
56 |
+
chat_history = chat_history or []
|
57 |
+
|
58 |
+
# Add user message
|
59 |
+
chat_history.append((query, None))
|
60 |
|
61 |
# Get AI response
|
62 |
+
bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac, query)
|
63 |
|
64 |
# Update chat history
|
65 |
+
chat_history[-1] = (query, bot_response)
|
66 |
|
67 |
return chat_history, chat_history
|
68 |
|
69 |
+
# Gradio UI
|
70 |
+
with gr.Blocks(
|
71 |
+
theme=gr.themes.Soft(
|
72 |
+
primary_hue="purple",
|
73 |
+
secondary_hue="pink"
|
74 |
+
),
|
75 |
+
title="Celestial Guide ๐",
|
76 |
+
css=".gradio-container {background: url('https://example.com/space-bg.jpg')}"
|
77 |
+
) as demo:
|
78 |
+
gr.Markdown("""
|
79 |
+
# ๐ Celestial Guide - AI Astrology Companion
|
80 |
+
*Your personal cosmic advisor powered by Groq & Llama-3*
|
81 |
+
""")
|
82 |
|
83 |
with gr.Row():
|
84 |
+
with gr.Column(scale=1, min_width=300):
|
85 |
+
gr.Markdown("## ๐ Birth Details")
|
86 |
+
name = gr.Textbox(label="Full Name", placeholder="Enter your full name...")
|
87 |
+
dob = gr.Textbox(label="Date of Birth (DD-MM-YYYY)", placeholder="e.g., 15-04-1990")
|
88 |
+
time_of_birth = gr.Textbox(label="Birth Time (HH:MM AM/PM)", placeholder="e.g., 08:45 PM")
|
89 |
+
place_of_birth = gr.Textbox(label="Birth Place", placeholder="City, Country")
|
90 |
zodiac = gr.Dropdown(
|
91 |
+
label="Sun Sign",
|
92 |
choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo",
|
93 |
"Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"],
|
94 |
+
value="Aries"
|
95 |
)
|
96 |
+
|
97 |
with gr.Column(scale=2):
|
98 |
+
gr.Markdown("## ๐ฌ Cosmic Consultation")
|
99 |
+
chatbot = gr.Chatbot(
|
100 |
+
height=600,
|
101 |
+
label="Astrology Chat",
|
102 |
+
avatar_images=("๐ค", "๐ฎ"),
|
103 |
+
bubble_full_width=False
|
104 |
+
)
|
105 |
+
query = gr.Textbox(
|
106 |
+
label="Your Cosmic Question",
|
107 |
+
placeholder="Ask about love, career, or spiritual growth...",
|
108 |
+
lines=3
|
109 |
+
)
|
110 |
+
with gr.Row():
|
111 |
+
submit_btn = gr.Button("Consult the Stars ๐ ", variant="primary")
|
112 |
+
clear_btn = gr.Button("New Session โป๏ธ")
|
113 |
+
|
114 |
+
state = gr.State()
|
115 |
|
116 |
+
# Event handlers
|
117 |
submit_btn.click(
|
118 |
+
handle_chat,
|
119 |
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
|
120 |
outputs=[chatbot, state]
|
121 |
)
|
122 |
+
|
123 |
+
query.submit(
|
124 |
+
handle_chat,
|
125 |
+
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
|
126 |
+
outputs=[chatbot, state]
|
127 |
+
)
|
128 |
+
|
129 |
+
clear_btn.click(lambda: (None, None, None, None, None, None, []),
|
130 |
+
outputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, chatbot])
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
+
demo.launch(share=False)
|