Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -144,59 +144,38 @@ TRAVEL PLAN:
|
|
| 144 |
return structured_package
|
| 145 |
|
| 146 |
|
|
|
|
| 147 |
class ChatRequest(BaseModel):
|
| 148 |
plan_text: str
|
| 149 |
user_query: str
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
plan_text = chat_input.plan_text
|
| 155 |
-
user_query = chat_input.user_query.lower()
|
| 156 |
|
| 157 |
-
|
| 158 |
-
return {
|
| 159 |
-
"response": "❌ I can only answer questions related to the travel plan or tours within Pakistan."
|
| 160 |
-
}
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
}
|
| 168 |
-
|
| 169 |
-
if "cost" in user_query or "price" in user_query or "budget" in user_query:
|
| 170 |
-
match = re.search(r"\*\*Total Estimated Cost:\*\*\s*(.*?)\n", plan_text)
|
| 171 |
-
return {
|
| 172 |
-
"response": f"The estimated cost is {match.group(1).strip()}." if match else "The cost isn't specified in the plan."
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
if "activities" in user_query or "things to do" in user_query or "highlights" in user_query:
|
| 176 |
-
match = re.search(r"\*\*Key Highlights and Attractions:\*\*\s*(.*?)\n", plan_text)
|
| 177 |
-
return {
|
| 178 |
-
"response": match.group(1).strip() if match else "Activities and highlights are not clearly mentioned."
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
if "food" in user_query or "meal" in user_query or "eat" in user_query or "restaurant" in user_query:
|
| 182 |
-
# Extract lunch or dinner mentions
|
| 183 |
-
food_section = re.findall(r"\*\*Afternoon.*?\*\*Lunch.*?:\*\*\s*(.*?)\n|\*\*Evening.*?\*\*Dinner.*?:\*\*\s*(.*?)\n", plan_text, re.DOTALL)
|
| 184 |
-
flattened = [item for tup in food_section for item in tup if item]
|
| 185 |
-
return {
|
| 186 |
-
"response": "Here’s what you can eat: " + " ".join(flattened) if flattened else "Food recommendations aren't available."
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
if "transport" in user_query or "how to get" in user_query:
|
| 190 |
-
match = re.search(r"\*\*Taxila to Murree:\*\*\s*(.*?)\n", plan_text)
|
| 191 |
-
return {
|
| 192 |
-
"response": match.group(1).strip() if match else "Transportation details are not mentioned."
|
| 193 |
-
}
|
| 194 |
-
|
| 195 |
-
# Fallback with section list
|
| 196 |
-
return {
|
| 197 |
-
"response": "I can help with specific parts of the travel plan like best time to visit, cost, transport, food, and highlights. What would you like to know?"
|
| 198 |
-
}
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
except Exception as e:
|
| 201 |
-
print("
|
| 202 |
raise HTTPException(status_code=500, detail="Failed to process your question.")
|
|
|
|
| 144 |
return structured_package
|
| 145 |
|
| 146 |
|
| 147 |
+
|
| 148 |
class ChatRequest(BaseModel):
|
| 149 |
plan_text: str
|
| 150 |
user_query: str
|
| 151 |
|
| 152 |
+
def create_gemini_prompt(plan_text: str, user_query: str) -> str:
|
| 153 |
+
return f"""
|
| 154 |
+
You are a helpful travel assistant specialized in tours and travel within Pakistan.
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
Here is a travel plan describing a trip:
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
\"\"\"
|
| 159 |
+
{plan_text}
|
| 160 |
+
\"\"\"
|
| 161 |
+
|
| 162 |
+
Please answer the user's question below **only if it is related to travel, sightseeing, food, transport, costs, culture, or activities in Pakistan based on the above plan**.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
User question:
|
| 165 |
+
{user_query}
|
| 166 |
+
|
| 167 |
+
If the question is unrelated to travel in Pakistan, respond politely with:
|
| 168 |
+
"I can only answer questions related to travel and tours within Pakistan."
|
| 169 |
+
|
| 170 |
+
Answer concisely and clearly.
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
@app.post("/chat", summary="Answer user questions about the generated travel plan")
|
| 174 |
+
async def chat_about_plan(chat_input: ChatRequest):
|
| 175 |
+
try:
|
| 176 |
+
prompt = create_gemini_prompt(chat_input.plan_text, chat_input.user_query)
|
| 177 |
+
llm_response = generate_with_gemini(prompt) # Remove 'await' if your wrapper is not async
|
| 178 |
+
return {"response": llm_response.strip()}
|
| 179 |
except Exception as e:
|
| 180 |
+
print("Error in chat endpoint:", e)
|
| 181 |
raise HTTPException(status_code=500, detail="Failed to process your question.")
|