Update app.py
Browse files
app.py
CHANGED
|
@@ -34,8 +34,7 @@ async def _extract_user_request(question: str) -> dict:
|
|
| 34 |
parser = JsonOutputParser(pydantic_object=TravelRequest)
|
| 35 |
prompt = ChatPromptTemplate.from_messages([
|
| 36 |
("system", "You are a travel assistant. Extract details."),
|
| 37 |
-
("human", "{format_instructions}
|
| 38 |
-
{request}")
|
| 39 |
]).partial(format_instructions=parser.get_format_instructions())
|
| 40 |
date_str = datetime.date.today().isoformat()
|
| 41 |
logging.info(f"Extracting: {question}")
|
|
@@ -110,40 +109,39 @@ async def _gather_travel_data(req):
|
|
| 110 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 111 |
combined = []
|
| 112 |
for r in results:
|
| 113 |
-
if not isinstance(r, Exception):
|
|
|
|
| 114 |
return combined
|
| 115 |
|
| 116 |
# 6. FORMAT OUTPUT
|
| 117 |
def _format_response(req, data):
|
| 118 |
dest = req['destination_city']
|
| 119 |
-
text = f"## Travel Plan for {dest}
|
| 120 |
-
"
|
| 121 |
flights = [i for i in data if i['type']=='flight']
|
| 122 |
hotels = [i for i in data if i['type']=='hotel']
|
| 123 |
acts = [i for i in data if i['type']=='activity']
|
|
|
|
| 124 |
if flights:
|
| 125 |
-
text += "
|
| 126 |
-
|
| 127 |
-
"
|
| 128 |
-
|
| 129 |
-
"
|
| 130 |
if hotels:
|
| 131 |
-
text += "
|
| 132 |
-
|
| 133 |
-
"
|
| 134 |
-
|
| 135 |
-
"
|
| 136 |
if acts:
|
| 137 |
-
text += "
|
| 138 |
-
|
| 139 |
-
"
|
| 140 |
-
|
| 141 |
-
"
|
| 142 |
return text
|
| 143 |
|
| 144 |
# 7. MAIN & UI
|
| 145 |
async def ask_bot(question):
|
| 146 |
-
if not question:
|
|
|
|
| 147 |
req = await _extract_user_request(question)
|
| 148 |
data = await _gather_travel_data(req)
|
| 149 |
return _format_response(req, data)
|
|
@@ -155,4 +153,5 @@ iface = gr.Interface(
|
|
| 155 |
title="AI Travel Bot"
|
| 156 |
)
|
| 157 |
|
| 158 |
-
if __name__ == "__main__":
|
|
|
|
|
|
| 34 |
parser = JsonOutputParser(pydantic_object=TravelRequest)
|
| 35 |
prompt = ChatPromptTemplate.from_messages([
|
| 36 |
("system", "You are a travel assistant. Extract details."),
|
| 37 |
+
("human", "{format_instructions}\\n{request}")
|
|
|
|
| 38 |
]).partial(format_instructions=parser.get_format_instructions())
|
| 39 |
date_str = datetime.date.today().isoformat()
|
| 40 |
logging.info(f"Extracting: {question}")
|
|
|
|
| 109 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 110 |
combined = []
|
| 111 |
for r in results:
|
| 112 |
+
if not isinstance(r, Exception):
|
| 113 |
+
combined.extend(r)
|
| 114 |
return combined
|
| 115 |
|
| 116 |
# 6. FORMAT OUTPUT
|
| 117 |
def _format_response(req, data):
|
| 118 |
dest = req['destination_city']
|
| 119 |
+
text = f"## Travel Plan for {dest}\n\n"
|
|
|
|
| 120 |
flights = [i for i in data if i['type']=='flight']
|
| 121 |
hotels = [i for i in data if i['type']=='hotel']
|
| 122 |
acts = [i for i in data if i['type']=='activity']
|
| 123 |
+
|
| 124 |
if flights:
|
| 125 |
+
text += "### Flights:\n"
|
| 126 |
+
for f in flights:
|
| 127 |
+
text += f"- {f['details']} (${f['price']}) [Book]({f['link']})\n"
|
| 128 |
+
|
|
|
|
| 129 |
if hotels:
|
| 130 |
+
text += "\n### Hotels:\n"
|
| 131 |
+
for h in hotels:
|
| 132 |
+
text += f"- {h['details']} (${h['price']}) [Book]({h['link']})\n"
|
| 133 |
+
|
|
|
|
| 134 |
if acts:
|
| 135 |
+
text += "\n### Activities:\n"
|
| 136 |
+
for a in acts:
|
| 137 |
+
text += f"- {a['details']} (${a['price']}) [Book]({a['link']})\n"
|
| 138 |
+
|
|
|
|
| 139 |
return text
|
| 140 |
|
| 141 |
# 7. MAIN & UI
|
| 142 |
async def ask_bot(question):
|
| 143 |
+
if not question:
|
| 144 |
+
return "Tell me your trip!"
|
| 145 |
req = await _extract_user_request(question)
|
| 146 |
data = await _gather_travel_data(req)
|
| 147 |
return _format_response(req, data)
|
|
|
|
| 153 |
title="AI Travel Bot"
|
| 154 |
)
|
| 155 |
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|