SatyamSinghal commited on
Commit
55d084b
ยท
verified ยท
1 Parent(s): 032c2d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -82
app.py CHANGED
@@ -1,36 +1,26 @@
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")
11
  openai.api_base = "https://api.groq.com/openai/v1"
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}
@@ -39,95 +29,65 @@ def get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac_sign, que
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)
 
1
  import gradio as gr
2
  import openai
3
  import os
 
 
 
 
4
 
5
  # Configure OpenAI API for Groq
6
+ openai.api_key = os.getenv("GROQ_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
+ system_prompt = f"""You are Master Celestia, an AI astrologer. Analyze this birth chart:
 
 
 
12
  Name: {name}
13
  DOB: {dob}
14
  Time: {time_of_birth}
15
  Place: {place_of_birth}
16
  Zodiac: {zodiac_sign}
17
 
18
+ Provide insights about:
19
+ 1. Current planetary transits
20
+ 2. Career and relationships
21
+ 3. Personalized recommendations
22
+ """
23
+
 
 
 
24
  messages = [
25
  {"role": "system", "content": system_prompt},
26
  {"role": "user", "content": query}
 
29
  response = openai.ChatCompletion.create(
30
  model="llama3-70b-8192",
31
  messages=messages,
32
+ temperature=0.7,
33
+ max_tokens=500
 
34
  )
35
  return response.choices[0].message["content"]
36
  except Exception as e:
37
+ return f"Error: {str(e)}"
38
 
39
+ # Renamed function to avoid conflict
40
  def handle_chat(name, dob, time_of_birth, place_of_birth, zodiac, query, chat_history):
 
 
 
 
 
41
  chat_history = chat_history or []
42
 
43
+ if query:
44
+ # Get AI response
45
+ bot_response = get_groq_response(name, dob, time_of_birth, place_of_birth, zodiac, query)
46
+ chat_history.append((query, bot_response))
 
 
 
 
47
 
48
  return chat_history, chat_history
49
 
50
+ with gr.Blocks(theme=gr.themes.Soft(), title="Astro Guide") as demo:
51
+ gr.Markdown("# ๐ŸŒŸ Astro Guide - AI Astrologer")
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  with gr.Row():
54
+ with gr.Column(scale=1):
55
+ name = gr.Textbox(label="Full Name")
56
+ dob = gr.Textbox(label="DOB (DD-MM-YYYY)")
57
+ time_of_birth = gr.Textbox(label="Birth Time")
58
+ place_of_birth = gr.Textbox(label="Birth Place")
 
59
  zodiac = gr.Dropdown(
 
60
  choices=["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo",
61
  "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"],
62
+ label="Sun Sign"
63
  )
64
+
65
  with gr.Column(scale=2):
66
+ # Renamed chatbot component
67
+ chat_interface = gr.Chatbot(height=500)
68
+ query = gr.Textbox(label="Your Question")
69
+ state = gr.State()
70
+ submit_btn = gr.Button("Ask the Stars", variant="primary")
71
+ clear_btn = gr.Button("Clear")
72
+
73
+ # Fixed click handlers
 
 
 
 
 
 
 
 
 
 
 
74
  submit_btn.click(
75
+ handle_chat, # Using the renamed function
76
  inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
77
+ outputs=[chat_interface, state]
78
  )
79
 
80
  query.submit(
81
  handle_chat,
82
  inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
83
+ outputs=[chat_interface, state]
84
  )
85
 
86
+ clear_btn.click(
87
+ lambda: (None, None, None, None, None, None, []),
88
+ outputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, chat_interface],
89
+ show_progress=False
90
+ )
91
 
92
  if __name__ == "__main__":
93
+ demo.launch()