Amitabhab commited on
Commit
f252e0f
·
1 Parent(s): 320fb70

Adding the ability to conversate with the trip itinerary

Browse files
Files changed (3) hide show
  1. app.py +92 -14
  2. config/tasks.yaml +2 -0
  3. crew.py +3 -2
app.py CHANGED
@@ -15,17 +15,44 @@ from typing import List, Tuple
15
 
16
  import gradio as gr
17
  import plotly.graph_objects as go
 
 
18
 
19
  from crew import AddressSummaryCrew, TravelCrew
20
  from db import log_query
21
  from fpdf import FPDF
22
 
23
- def export_pdf(input_text:str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  """
25
  Create a downloadable pdf for the given input text
26
 
27
  Args:
28
  input_text: The text that needs to be made a pdf
 
29
 
30
  Result:
31
  Downloadable pdf
@@ -46,6 +73,21 @@ def export_pdf(input_text:str):
46
  pdf.set_font("Arial", size=12, style='')
47
  else:
48
  pdf.multi_cell(0, 5, clean_line)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  pdf.output(file_name).encode('latin-1')
50
  return file_name
51
 
@@ -140,7 +182,12 @@ def run(
140
  'children': children,
141
  'budget': budget,
142
  }
143
- result = TravelCrew(model_name).crew().kickoff(inputs=user_preferences)
 
 
 
 
 
144
 
145
  """
146
  Now we will pass the result to a address summary crew whose job is to extract position
@@ -185,6 +232,8 @@ logger.setLevel(logging.INFO)
185
  with gr.Blocks() as demo:
186
  gr.Markdown('Use this app to create a detailed itinerary on how to explore a new place.'
187
  ' Itinerary is customized to your taste. Powered by Sambanova Cloud.')
 
 
188
  with gr.Row():
189
  with gr.Column(scale=1):
190
  inp_source = gr.Textbox(label='Where are you travelling from?')
@@ -227,24 +276,53 @@ with gr.Blocks() as demo:
227
  label='Total budget of trip in USD', show_label=True, value=1000, minimum=500, maximum=10000, step=500
228
  )
229
  inp_model = gr.Textbox(value="Meta-Llama-3.1-70B-Instruct", label='Sambanova Model Name')
230
- button = gr.Button("Plan your Trip")
231
- inputs = [inp_source, inp_dest, inp_cal, inp_age, inp_days, inp_interests, inp_cuisine, inp_children, inp_budget, inp_model]
232
 
233
  with gr.Column(scale=2):
234
- output_itinerary =\
235
- gr.Textbox(
236
- label='Complete Personalized Itinerary of your Trip',
237
- show_label=True,
238
- show_copy_button=True,
239
- autoscroll=False,
240
- )
 
 
 
 
 
 
 
 
 
 
 
241
  download_btn = gr.Button("Download Itinerary")
242
 
243
  output_map = gr.Plot(label='Venues on a Map. Please verify with a Navigation System before traveling.')
244
  output = [output_itinerary, output_map]
245
 
246
- button.click(fn=run, inputs=inputs, outputs=output)
247
- download_btn_hidden = gr.DownloadButton(visible=False, elem_id="download_btn_hidden")
248
- download_btn.click(fn=export_pdf, inputs=output_itinerary, outputs=[download_btn_hidden]).then(fn=None, inputs=None, outputs=None, js="() => document.querySelector('#download_btn_hidden').click()")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
  demo.launch()
 
15
 
16
  import gradio as gr
17
  import plotly.graph_objects as go
18
+ import os
19
+ import openai
20
 
21
  from crew import AddressSummaryCrew, TravelCrew
22
  from db import log_query
23
  from fpdf import FPDF
24
 
25
+ client = openai.OpenAI(
26
+ api_key=os.environ.get("SAMBANOVA_API_KEY"),
27
+ base_url="https://api.sambanova.ai/v1",
28
+ )
29
+
30
+ def start_chat(context):
31
+ return gr.Chatbot(visible=True), gr.Textbox(visible=True), context
32
+
33
+ def respond(message, chat_history, context, model="Meta-Llama-3.1-70B-Instruct"):
34
+ # Simple response incorporating context
35
+ response = client.chat.completions.create(
36
+ model=model,
37
+ messages=[{"role":"system",
38
+ "content":"You are a helpful assistant"},
39
+ {"role": "user",
40
+ "content": "Here is a trip itinerary: %s. Please answer the specific question asked by the user. %s " % (message, context)}],
41
+ temperature=0.1,
42
+ top_p=0.1
43
+ )
44
+ result = response.choices[0].message.content
45
+ bot_message = result
46
+ chat_history.append((message, bot_message))
47
+ return "", chat_history
48
+
49
+ def export_pdf(input_text:str, input_chat:str):
50
  """
51
  Create a downloadable pdf for the given input text
52
 
53
  Args:
54
  input_text: The text that needs to be made a pdf
55
+ input_chat: Chat messages
56
 
57
  Result:
58
  Downloadable pdf
 
73
  pdf.set_font("Arial", size=12, style='')
74
  else:
75
  pdf.multi_cell(0, 5, clean_line)
76
+
77
+ pdf.ln()
78
+ for conversation in input_chat:
79
+ counter = 0
80
+ for line in conversation:
81
+ clean_line = line.strip()
82
+ if clean_line:
83
+ if counter == 0:
84
+ pdf.ln()
85
+ pdf.set_font("Arial", size=12, style='I')
86
+ counter += 1
87
+ else:
88
+ pdf.set_font("Arial", size=12, style='')
89
+ pdf.multi_cell(0, 5, clean_line)
90
+
91
  pdf.output(file_name).encode('latin-1')
92
  return file_name
93
 
 
182
  'children': children,
183
  'budget': budget,
184
  }
185
+ #result = TravelCrew(model_name).crew().kickoff(inputs=user_preferences)
186
+ crew = TravelCrew(model_name).crew()
187
+ result = crew.kickoff(inputs=user_preferences)
188
+ metrics = crew.usage_metrics
189
+ logger.info("Result Metrics")
190
+ logger.info(metrics)
191
 
192
  """
193
  Now we will pass the result to a address summary crew whose job is to extract position
 
232
  with gr.Blocks() as demo:
233
  gr.Markdown('Use this app to create a detailed itinerary on how to explore a new place.'
234
  ' Itinerary is customized to your taste. Powered by Sambanova Cloud.')
235
+ # Store context between interactions
236
+ context = gr.State()
237
  with gr.Row():
238
  with gr.Column(scale=1):
239
  inp_source = gr.Textbox(label='Where are you travelling from?')
 
276
  label='Total budget of trip in USD', show_label=True, value=1000, minimum=500, maximum=10000, step=500
277
  )
278
  inp_model = gr.Textbox(value="Meta-Llama-3.1-70B-Instruct", label='Sambanova Model Name')
279
+ plan_button = gr.Button("Plan your Trip")
280
+ inputs = [inp_source, inp_dest, inp_cal, inp_age, inp_days, inp_interests, inp_cuisine, inp_children, inp_budget, inp_model]
281
 
282
  with gr.Column(scale=2):
283
+ with gr.Row():
284
+ output_itinerary =\
285
+ gr.Textbox(
286
+ label='Complete Personalized Itinerary of your Trip',
287
+ show_label=True,
288
+ show_copy_button=True,
289
+ autoscroll=False,
290
+ )
291
+
292
+ # Chat interface (hidden initially)
293
+ with gr.Row(visible=False) as chat_interface:
294
+ chatbot = gr.Chatbot()
295
+ input_msg = gr.Textbox()
296
+
297
+ # Chat controls
298
+ start_chat_btn = gr.Button("Start Chat", visible=False)
299
+
300
+ # Download button
301
  download_btn = gr.Button("Download Itinerary")
302
 
303
  output_map = gr.Plot(label='Venues on a Map. Please verify with a Navigation System before traveling.')
304
  output = [output_itinerary, output_map]
305
 
306
+ plan_button.click(fn=run, inputs=inputs, outputs=output).then(
307
+ lambda: gr.Button(visible=True),
308
+ outputs=start_chat_btn)
309
+
310
+ download_btn_hidden = gr.DownloadButton(visible=False, elem_id="download_btn_hidden")
311
+ download_btn.click(fn=export_pdf, inputs=[output_itinerary, chatbot], outputs=[download_btn_hidden]).then(fn=None, inputs=None, outputs=None, js="() => document.querySelector('#download_btn_hidden').click()")
312
+
313
+ start_chat_btn.click(
314
+ start_chat,
315
+ inputs=output_itinerary,
316
+ outputs=[chatbot, input_msg, context]
317
+ ).then(
318
+ lambda: gr.Row(visible=True),
319
+ outputs=chat_interface
320
+ )
321
+
322
+ input_msg.submit(
323
+ respond,
324
+ inputs=[input_msg, chatbot, context, inp_model],
325
+ outputs=[input_msg, chatbot]
326
+ )
327
 
328
  demo.launch()
config/tasks.yaml CHANGED
@@ -5,6 +5,7 @@ personalized_activity_planning_task:
5
  Also map the date to the day of the week and check if the activity you are recommending in open on that day of the week.
6
  Focus on activities and events that match the traveler's interests and age group.
7
  Utilize internet search tools and recommendation engines to gather the information.
 
8
 
9
  Traveler's information:
10
 
@@ -39,6 +40,7 @@ restaurant_scenic_location_scout_task:
39
  Use internet search tools, restaurant review sites, and travel guides.
40
  Make sure to find a variety of options to suit different tastes and budgets, and ratings for them.
41
  Extract the address of he restaurant so that the same can be displayed to the user.
 
42
 
43
  Traveler's information:
44
 
 
5
  Also map the date to the day of the week and check if the activity you are recommending in open on that day of the week.
6
  Focus on activities and events that match the traveler's interests and age group.
7
  Utilize internet search tools and recommendation engines to gather the information.
8
+ If you are including a hotel for staying, please provide a link to make a booking at the hotel.
9
 
10
  Traveler's information:
11
 
 
40
  Use internet search tools, restaurant review sites, and travel guides.
41
  Make sure to find a variety of options to suit different tastes and budgets, and ratings for them.
42
  Extract the address of he restaurant so that the same can be displayed to the user.
43
+ If it is possible, provide a link to make an online reseravtion at the restaurant.
44
 
45
  Traveler's information:
46
 
crew.py CHANGED
@@ -20,6 +20,7 @@ class TravelCrew:
20
  self.agents_config = 'config/agents.yaml'
21
  self.tasks_config = 'config/tasks.yaml'
22
  self.llm = LLM(model='sambanova/%s' % model_name)
 
23
 
24
  @typing.no_type_check
25
  @agent
@@ -131,7 +132,7 @@ class TravelCrew:
131
  @task
132
  def itinerary_compilation_task(self) -> Task:
133
  """
134
- A task that plans for museums.
135
 
136
  Returns: A task
137
  """
@@ -153,7 +154,7 @@ class TravelCrew:
153
  return Crew(
154
  agents=self.agents,
155
  tasks=self.tasks,
156
- process=Process.sequential,
157
  )
158
 
159
 
 
20
  self.agents_config = 'config/agents.yaml'
21
  self.tasks_config = 'config/tasks.yaml'
22
  self.llm = LLM(model='sambanova/%s' % model_name)
23
+ self.manager_llm = LLM(model='sambanova/%s' % model_name)
24
 
25
  @typing.no_type_check
26
  @agent
 
132
  @task
133
  def itinerary_compilation_task(self) -> Task:
134
  """
135
+ A task that plans the complete itinerary
136
 
137
  Returns: A task
138
  """
 
154
  return Crew(
155
  agents=self.agents,
156
  tasks=self.tasks,
157
+ process=Process.sequential
158
  )
159
 
160