SatyamSinghal commited on
Commit
12947cf
ยท
verified ยท
1 Parent(s): 3886dff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -36
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": 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()
 
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)