Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
# -----------------
|
| 6 |
-
#
|
| 7 |
-
# -----------------
|
| 8 |
-
generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 9 |
-
|
| 10 |
-
# -----------------
|
| 11 |
-
# Zodiac helper
|
| 12 |
# -----------------
|
| 13 |
def get_zodiac_sign(date_str):
|
| 14 |
try:
|
|
@@ -18,92 +12,74 @@ def get_zodiac_sign(date_str):
|
|
| 18 |
return "Unknown"
|
| 19 |
|
| 20 |
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
|
| 21 |
-
return "Aries "
|
| 22 |
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
|
| 23 |
-
return "Taurus "
|
| 24 |
elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
|
| 25 |
-
return "Gemini "
|
| 26 |
elif (month == 6 and day >= 21) or (month == 7 and day <= 22):
|
| 27 |
-
return "Cancer "
|
| 28 |
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
|
| 29 |
-
return "Leo "
|
| 30 |
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
|
| 31 |
-
return "Virgo "
|
| 32 |
elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
|
| 33 |
-
return "Libra "
|
| 34 |
elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
|
| 35 |
-
return "Scorpio "
|
| 36 |
elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
|
| 37 |
-
return "Sagittarius "
|
| 38 |
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
|
| 39 |
-
return "Capricorn "
|
| 40 |
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
|
| 41 |
-
return "Aquarius "
|
| 42 |
elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
|
| 43 |
-
return "Pisces "
|
| 44 |
return "Unknown"
|
| 45 |
|
| 46 |
-
# -----------------
|
| 47 |
-
# Functions
|
| 48 |
-
# -----------------
|
| 49 |
def generate_prediction(name, date, time, place):
|
| 50 |
zodiac = get_zodiac_sign(date)
|
| 51 |
return f"โจ Hello {name}, based on your birth details, your zodiac sign is {zodiac}. " \
|
| 52 |
-
f"You
|
| 53 |
|
| 54 |
def answer_question(name, date, time, place, question):
|
|
|
|
| 55 |
zodiac = get_zodiac_sign(date)
|
| 56 |
-
prompt = f"""
|
| 57 |
-
You are a knowledgeable astrologer.
|
| 58 |
-
Person's name: {name}
|
| 59 |
-
Birth place: {place}
|
| 60 |
-
Zodiac sign: {zodiac}.
|
| 61 |
-
The person asks: "{question}".
|
| 62 |
-
|
| 63 |
-
Give a thoughtful astrology-based response that sounds mystical,
|
| 64 |
-
but stays positive and helpful. Do not repeat the question.
|
| 65 |
-
Avoid saying the person will literally become an astrologer.
|
| 66 |
-
"""
|
| 67 |
-
result = generator(prompt, max_length=200, temperature=0.7)
|
| 68 |
-
return result[0]["generated_text"]
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# -----------------
|
| 72 |
-
#
|
| 73 |
# -----------------
|
| 74 |
-
with gr.Blocks(
|
| 75 |
-
|
| 76 |
-
.
|
| 77 |
-
.card {background: white; padding: 20px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.2);}
|
| 78 |
-
h1 {text-align: center; color: #fff; font-size: 2em; margin-bottom: 10px;}
|
| 79 |
-
h2 {color: #333;}
|
| 80 |
-
""") as demo:
|
| 81 |
-
|
| 82 |
-
gr.Markdown("<h1>AI Astrologer </h1>")
|
| 83 |
-
gr.Markdown("<p style='text-align:center; color:white;'>Enter your details and ask the stars for guidance </p>")
|
| 84 |
|
| 85 |
-
with gr.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
date = gr.Textbox(label="Birth Date (YYYY-MM-DD)")
|
| 91 |
-
time = gr.Textbox(label="Birth Time (HH:MM)", placeholder="Optional")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
with gr.Column(elem_classes="card"):
|
| 97 |
-
gr.Markdown("### Ask the Stars")
|
| 98 |
-
question = gr.Textbox(label="What do you want to ask?", placeholder="e.g. How is my career looking?")
|
| 99 |
-
ask_btn = gr.Button("Ask Astrologer", variant="secondary")
|
| 100 |
-
answer_output = gr.Textbox(label="Astrology Answer")
|
| 101 |
-
|
| 102 |
-
# Button actions
|
| 103 |
prediction_btn.click(fn=generate_prediction,
|
| 104 |
inputs=[name, date, time, place],
|
| 105 |
outputs=prediction_output)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
ask_btn.click(fn=answer_question,
|
| 108 |
inputs=[name, date, time, place, question],
|
| 109 |
outputs=answer_output)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
# -----------------
|
| 5 |
+
# Rule-based astrology logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# -----------------
|
| 7 |
def get_zodiac_sign(date_str):
|
| 8 |
try:
|
|
|
|
| 12 |
return "Unknown"
|
| 13 |
|
| 14 |
if (month == 3 and day >= 21) or (month == 4 and day <= 19):
|
| 15 |
+
return "Aries โ"
|
| 16 |
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
|
| 17 |
+
return "Taurus โ"
|
| 18 |
elif (month == 5 and day >= 21) or (month == 6 and day <= 20):
|
| 19 |
+
return "Gemini โ"
|
| 20 |
elif (month == 6 and day >= 21) or (month == 7 and day <= 22):
|
| 21 |
+
return "Cancer โ"
|
| 22 |
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
|
| 23 |
+
return "Leo โ"
|
| 24 |
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
|
| 25 |
+
return "Virgo โ"
|
| 26 |
elif (month == 9 and day >= 23) or (month == 10 and day <= 22):
|
| 27 |
+
return "Libra โ"
|
| 28 |
elif (month == 10 and day >= 23) or (month == 11 and day <= 21):
|
| 29 |
+
return "Scorpio โ"
|
| 30 |
elif (month == 11 and day >= 22) or (month == 12 and day <= 21):
|
| 31 |
+
return "Sagittarius โ"
|
| 32 |
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
|
| 33 |
+
return "Capricorn โ"
|
| 34 |
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
|
| 35 |
+
return "Aquarius โ"
|
| 36 |
elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
|
| 37 |
+
return "Pisces โ"
|
| 38 |
return "Unknown"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
def generate_prediction(name, date, time, place):
|
| 41 |
zodiac = get_zodiac_sign(date)
|
| 42 |
return f"โจ Hello {name}, based on your birth details, your zodiac sign is {zodiac}. " \
|
| 43 |
+
f"You are likely to find new opportunities around {place}. Trust your instincts and stay positive!"
|
| 44 |
|
| 45 |
def answer_question(name, date, time, place, question):
|
| 46 |
+
q = question.lower()
|
| 47 |
zodiac = get_zodiac_sign(date)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if "career" in q:
|
| 50 |
+
return f"๐ {name}, as a {zodiac}, your career stars indicate steady growth. Hard work will pay off soon!"
|
| 51 |
+
elif "love" in q or "relationship" in q:
|
| 52 |
+
return f"๐ {name}, love energies are strong for {zodiac}. Be open to new connections."
|
| 53 |
+
elif "health" in q:
|
| 54 |
+
return f"๐ช {name}, {zodiac} energy suggests you focus on balance. Prioritize rest and self-care."
|
| 55 |
+
else:
|
| 56 |
+
return f"๐ฎ The universe whispers patience, {name}. As a {zodiac}, trust the timing of your journey."
|
| 57 |
|
| 58 |
# -----------------
|
| 59 |
+
# Gradio UI
|
| 60 |
# -----------------
|
| 61 |
+
with gr.Blocks(theme="default") as demo:
|
| 62 |
+
gr.Markdown("## โจ AI Astrologer ๐ฎ")
|
| 63 |
+
gr.Markdown("Enter your birth details and receive astrology-based insights.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Row():
|
| 66 |
+
name = gr.Textbox(label="Name")
|
| 67 |
+
date = gr.Textbox(label="Birth Date (YYYY-MM-DD)")
|
| 68 |
+
time = gr.Textbox(label="Birth Time (HH:MM)", placeholder="Optional")
|
| 69 |
+
place = gr.Textbox(label="Birth Place")
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
prediction_btn = gr.Button("Get Prediction")
|
| 72 |
+
prediction_output = gr.Textbox(label="Prediction")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
prediction_btn.click(fn=generate_prediction,
|
| 75 |
inputs=[name, date, time, place],
|
| 76 |
outputs=prediction_output)
|
| 77 |
|
| 78 |
+
gr.Markdown("### โ Ask a Free Question")
|
| 79 |
+
question = gr.Textbox(label="Your Question")
|
| 80 |
+
answer_output = gr.Textbox(label="Astrology Answer")
|
| 81 |
+
ask_btn = gr.Button("Ask the Astrologer")
|
| 82 |
+
|
| 83 |
ask_btn.click(fn=answer_question,
|
| 84 |
inputs=[name, date, time, place, question],
|
| 85 |
outputs=answer_output)
|