Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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("
|
| 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 |
-
|
| 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
|
| 26 |
-
1. Current planetary transits
|
| 27 |
-
2. Career and
|
| 28 |
-
3.
|
| 29 |
-
|
| 30 |
-
|
| 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.
|
| 43 |
-
max_tokens=
|
| 44 |
-
top_p=0.9
|
| 45 |
)
|
| 46 |
return response.choices[0].message["content"]
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"
|
| 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 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
| 70 |
-
|
| 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
|
| 85 |
-
gr.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 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 |
-
|
| 95 |
)
|
| 96 |
-
|
| 97 |
with gr.Column(scale=2):
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 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=[
|
| 121 |
)
|
| 122 |
|
| 123 |
query.submit(
|
| 124 |
handle_chat,
|
| 125 |
inputs=[name, dob, time_of_birth, place_of_birth, zodiac, query, state],
|
| 126 |
-
outputs=[
|
| 127 |
)
|
| 128 |
|
| 129 |
-
clear_btn.click(
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
demo.launch(
|
|
|
|
| 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()
|