Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,12 +19,11 @@ from langchain_openai import ChatOpenAI
|
|
| 19 |
from langgraph.prebuilt import create_react_agent
|
| 20 |
|
| 21 |
# ==============================================================================
|
| 22 |
-
# PART 1: TOOL DEFINITIONS
|
| 23 |
# ==============================================================================
|
| 24 |
@tool
|
| 25 |
def get_horoscope(sign: str, date: str = None, language: str = "EN") -> str:
|
| 26 |
"""Fetches the horoscope for a given zodiac sign and date."""
|
| 27 |
-
# ... (Your full get_horoscope function code)
|
| 28 |
try:
|
| 29 |
if date: date_obj = parser.parse(date)
|
| 30 |
else: date_obj = datetime.datetime.now()
|
|
@@ -39,7 +38,6 @@ def get_horoscope(sign: str, date: str = None, language: str = "EN") -> str:
|
|
| 39 |
@tool
|
| 40 |
def get_date_panchang(date: str = None, data_language: str = "EN") -> str:
|
| 41 |
"""Fetches the Panchang data for a given date."""
|
| 42 |
-
# ... (Your full get_date_panchang function code)
|
| 43 |
try:
|
| 44 |
if not date: now = datetime.datetime.now()
|
| 45 |
else: now = parser.parse(date)
|
|
@@ -54,7 +52,6 @@ def get_date_panchang(date: str = None, data_language: str = "EN") -> str:
|
|
| 54 |
@tool
|
| 55 |
def get_holidays(year: int = None, data_language: str = "EN") -> str:
|
| 56 |
"""Fetches holidays for a given year."""
|
| 57 |
-
# ... (Your full get_holidays function code)
|
| 58 |
try:
|
| 59 |
if not year: year = datetime.datetime.now().year
|
| 60 |
params = {"data_language": data_language, "year": year}
|
|
@@ -67,7 +64,6 @@ def get_holidays(year: int = None, data_language: str = "EN") -> str:
|
|
| 67 |
@tool
|
| 68 |
def get_monthly_festivals(year: Optional[int] = None, month: Optional[str] = None, data_language: str = "EN") -> str:
|
| 69 |
"""Fetches festival data for a specific month and year."""
|
| 70 |
-
# ... (Your full get_monthly_festivals function code)
|
| 71 |
try:
|
| 72 |
if not year: year = datetime.datetime.now().year
|
| 73 |
if not month: month = datetime.datetime.now().strftime("%B").lower()
|
|
@@ -83,56 +79,51 @@ def get_monthly_festivals(year: Optional[int] = None, month: Optional[str] = Non
|
|
| 83 |
all_tools = [get_horoscope, get_date_panchang, get_holidays, get_monthly_festivals]
|
| 84 |
|
| 85 |
# ==============================================================================
|
| 86 |
-
# PART 2: SYSTEM PROMPT
|
| 87 |
# ==============================================================================
|
| 88 |
-
SYSTEM_PROMPT = """ Your primary function is to use the provided tools to answer user queries accurately.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
**Final Response Style:**
|
| 123 |
-
Your final answer to the user should always be friendly, well-formatted, and directly address their question using only the data you retrieved from the tools.
|
| 124 |
-
"""
|
| 125 |
|
| 126 |
# ==============================================================================
|
| 127 |
-
# PART 3: MODEL INITIALIZATION
|
| 128 |
# ==============================================================================
|
| 129 |
load_dotenv()
|
| 130 |
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY")
|
| 131 |
|
| 132 |
if not SARVAM_API_KEY:
|
| 133 |
print("Warning: SARVAM_API_KEY secret not found.")
|
| 134 |
-
# In a deployed space, we can't raise an error, so we'll let it fail later
|
| 135 |
-
# raise ValueError("SARVAM_API_KEY not found. Please set it in the Space secrets.")
|
| 136 |
|
| 137 |
model = ChatOpenAI(
|
| 138 |
model="sarvam-m",
|
|
@@ -143,9 +134,9 @@ model = ChatOpenAI(
|
|
| 143 |
print("Sarvam AI model initialized successfully.")
|
| 144 |
|
| 145 |
# ==============================================================================
|
| 146 |
-
# PART 4: AGENT CREATION
|
| 147 |
# ==============================================================================
|
| 148 |
-
agent_app = create_react_agent(model=model, tools=all_tools,
|
| 149 |
print("Pre-built ReAct agent created successfully.")
|
| 150 |
|
| 151 |
# ==============================================================================
|
|
@@ -179,12 +170,8 @@ chatbot_ui = gr.ChatInterface(
|
|
| 179 |
fn=get_agent_response,
|
| 180 |
title="Jyotish AI Assistant",
|
| 181 |
description="Ask about horoscopes, panchang, holidays, and festivals.",
|
| 182 |
-
type="messages"
|
| 183 |
-
examples
|
| 184 |
-
"What is the horoscope for Leo today?",
|
| 185 |
-
"Tell me the panchang for today",
|
| 186 |
-
"What are the Indian holidays in 2025?"
|
| 187 |
-
]
|
| 188 |
)
|
| 189 |
|
| 190 |
# Launch the Gradio app
|
|
|
|
| 19 |
from langgraph.prebuilt import create_react_agent
|
| 20 |
|
| 21 |
# ==============================================================================
|
| 22 |
+
# PART 1: TOOL DEFINITIONS
|
| 23 |
# ==============================================================================
|
| 24 |
@tool
|
| 25 |
def get_horoscope(sign: str, date: str = None, language: str = "EN") -> str:
|
| 26 |
"""Fetches the horoscope for a given zodiac sign and date."""
|
|
|
|
| 27 |
try:
|
| 28 |
if date: date_obj = parser.parse(date)
|
| 29 |
else: date_obj = datetime.datetime.now()
|
|
|
|
| 38 |
@tool
|
| 39 |
def get_date_panchang(date: str = None, data_language: str = "EN") -> str:
|
| 40 |
"""Fetches the Panchang data for a given date."""
|
|
|
|
| 41 |
try:
|
| 42 |
if not date: now = datetime.datetime.now()
|
| 43 |
else: now = parser.parse(date)
|
|
|
|
| 52 |
@tool
|
| 53 |
def get_holidays(year: int = None, data_language: str = "EN") -> str:
|
| 54 |
"""Fetches holidays for a given year."""
|
|
|
|
| 55 |
try:
|
| 56 |
if not year: year = datetime.datetime.now().year
|
| 57 |
params = {"data_language": data_language, "year": year}
|
|
|
|
| 64 |
@tool
|
| 65 |
def get_monthly_festivals(year: Optional[int] = None, month: Optional[str] = None, data_language: str = "EN") -> str:
|
| 66 |
"""Fetches festival data for a specific month and year."""
|
|
|
|
| 67 |
try:
|
| 68 |
if not year: year = datetime.datetime.now().year
|
| 69 |
if not month: month = datetime.datetime.now().strftime("%B").lower()
|
|
|
|
| 79 |
all_tools = [get_horoscope, get_date_panchang, get_holidays, get_monthly_festivals]
|
| 80 |
|
| 81 |
# ==============================================================================
|
| 82 |
+
# PART 2: SYSTEM PROMPT
|
| 83 |
# ==============================================================================
|
| 84 |
+
SYSTEM_PROMPT = """You are an expert astrological and calendar assistant. Your primary function is to use the provided tools to answer user queries accurately.
|
| 85 |
+
|
| 86 |
+
**Core Directives:**
|
| 87 |
+
1. **Tool First:** You MUST use the provided tools to find information. Never answer from your own general knowledge. If the tools do not provide an answer, state that the information could not be found.
|
| 88 |
+
2. **Language Match:** You MUST respond in the exact language of the user's query. You are proficient in English (EN), Hindi (HI), Bengali (BN), Gujarati (GU), Tamil (TA), Telugu (TE), Kannada (KN), Malayalam (ML), Marathi (MR), Oriya (OR), and Panjabi (PA).
|
| 89 |
+
3. **Interpret, Don't Dump:** Your job is to interpret the JSON data returned by the tools and present it to the user in a clear, well-formatted, and human-readable way. Do not just output the raw JSON.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
**Tool Usage and Data Interpretation Guide:**
|
| 94 |
+
|
| 95 |
+
**1. `get_horoscope`**
|
| 96 |
+
- **When to Use:** Use this tool when a user asks for a horoscope for any zodiac sign (e.g., Aries, Leo, Gemini).
|
| 97 |
+
- **Data Interpretation:** The tool returns a JSON object with keys like `prediction`, `monetary_gains`, `love_life`, `health`, `lucky_number`, and `lucky_color`. Format your response using clear headings for each of these categories.
|
| 98 |
+
|
| 99 |
+
**2. `get_date_panchang`**
|
| 100 |
+
- **When to Use:** Use this tool when a user asks for the "Panchang," "Panchangam," or detailed daily astrological details for a specific date.
|
| 101 |
+
- **Data Interpretation:** This tool returns a very large JSON object. **Do not dump the entire object.**
|
| 102 |
+
- If the user asks for the general Panchang, summarize the most important elements: **Sunrise, Sunset, Tithi, Nakshatra, Yoga, and Karana**.
|
| 103 |
+
- If the user asks for a specific detail (e.g., "What is Rahu Kalam today?"), find that specific key in the JSON (`Rahu Kalam`) and provide only that information.
|
| 104 |
+
|
| 105 |
+
**3. `get_holidays`**
|
| 106 |
+
- **When to Use:** Use this tool for general queries about holidays within a specific **year**.
|
| 107 |
+
- **Data Interpretation:** Present the holidays in a clean list format.
|
| 108 |
+
|
| 109 |
+
**4. `get_monthly_festivals`**
|
| 110 |
+
- **When to Use:** Prefer this tool when a user asks for festivals in a specific **month**. It provides more detail than `get_holidays`.
|
| 111 |
+
- **Data Interpretation:** Format the response as a list of festivals for that month, including the date for each.
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
**Final Response Style:**
|
| 116 |
+
Your final answer to the user should always be friendly, well-formatted, and directly address their question using only the data you retrieved from the tools.
|
| 117 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# ==============================================================================
|
| 120 |
+
# PART 3: MODEL INITIALIZATION
|
| 121 |
# ==============================================================================
|
| 122 |
load_dotenv()
|
| 123 |
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY")
|
| 124 |
|
| 125 |
if not SARVAM_API_KEY:
|
| 126 |
print("Warning: SARVAM_API_KEY secret not found.")
|
|
|
|
|
|
|
| 127 |
|
| 128 |
model = ChatOpenAI(
|
| 129 |
model="sarvam-m",
|
|
|
|
| 134 |
print("Sarvam AI model initialized successfully.")
|
| 135 |
|
| 136 |
# ==============================================================================
|
| 137 |
+
# PART 4: AGENT CREATION
|
| 138 |
# ==============================================================================
|
| 139 |
+
agent_app = create_react_agent(model=model, tools=all_tools, system_message=SYSTEM_PROMPT)
|
| 140 |
print("Pre-built ReAct agent created successfully.")
|
| 141 |
|
| 142 |
# ==============================================================================
|
|
|
|
| 170 |
fn=get_agent_response,
|
| 171 |
title="Jyotish AI Assistant",
|
| 172 |
description="Ask about horoscopes, panchang, holidays, and festivals.",
|
| 173 |
+
type="messages"
|
| 174 |
+
# The 'examples' line has been removed to prevent the startup crash.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
# Launch the Gradio app
|