pleabargain commited on
Commit
a90761c
·
verified ·
1 Parent(s): 1777fd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -52
app.py CHANGED
@@ -1,64 +1,216 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
- respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ import datetime
2
+ from typing import Dict, List, Tuple
3
+ import random
4
 
5
+ class TravelPlanner:
6
+ def __init__(self):
7
+ self.accommodation_types = {
8
+ 'luxury': (200, 500),
9
+ 'mid_range': (100, 200),
10
+ 'budget': (50, 100),
11
+ 'hostel': (20, 50)
12
+ }
13
+
14
+ self.activity_costs = {
15
+ 'sightseeing': (20, 50),
16
+ 'museum': (15, 25),
17
+ 'adventure_sport': (50, 200),
18
+ 'local_tour': (30, 80),
19
+ 'cultural_experience': (25, 60)
20
+ }
21
+
22
+ self.meal_costs = {
23
+ 'luxury': (50, 100),
24
+ 'mid_range': (20, 50),
25
+ 'budget': (10, 20),
26
+ 'street_food': (5, 10)
27
+ }
28
 
29
+ def generate_itinerary(self,
30
+ destination: str,
31
+ num_days: int,
32
+ budget_level: str) -> List[Dict]:
33
+ """
34
+ Generate a daily itinerary based on destination and budget level
35
+
36
+ Args:
37
+ destination: Name of the destination
38
+ num_days: Number of days for the trip
39
+ budget_level: luxury/mid_range/budget/hostel
40
+
41
+ Returns:
42
+ List of daily activities and estimated times
43
+ """
44
+ itinerary = []
45
+
46
+ activities = [
47
+ 'Morning sightseeing',
48
+ 'Museum visit',
49
+ 'Local market tour',
50
+ 'Cultural workshop',
51
+ 'Historical site visit',
52
+ 'Nature walk',
53
+ 'Local neighborhood exploration',
54
+ 'Evening entertainment'
55
+ ]
56
+
57
+ for day in range(1, num_days + 1):
58
+ daily_schedule = {
59
+ 'day': day,
60
+ 'morning': random.choice(activities),
61
+ 'afternoon': random.choice(activities),
62
+ 'evening': random.choice(activities),
63
+ 'accommodation': f"{budget_level} accommodation"
64
+ }
65
+ itinerary.append(daily_schedule)
66
+
67
+ return itinerary
68
 
69
+ def calculate_budget(self,
70
+ num_days: int,
71
+ budget_level: str,
72
+ num_people: int) -> Dict:
73
+ """
74
+ Calculate estimated budget based on duration and comfort level
75
+
76
+ Args:
77
+ num_days: Number of days for the trip
78
+ budget_level: luxury/mid_range/budget/hostel
79
+ num_people: Number of travelers
80
+
81
+ Returns:
82
+ Dictionary with budget breakdown
83
+ """
84
+ # Get cost ranges based on budget level
85
+ accommodation_range = self.accommodation_types[budget_level]
86
+ meal_range = self.meal_costs[budget_level]
87
+
88
+ # Calculate daily costs
89
+ daily_accommodation = random.uniform(*accommodation_range)
90
+ daily_meals = random.uniform(*meal_range) * 3 # 3 meals per day
91
+ daily_activities = random.uniform(30, 100) # Average activity cost
92
+ daily_transport = random.uniform(10, 50) # Local transport
93
+
94
+ # Calculate total costs
95
+ total_accommodation = daily_accommodation * num_days
96
+ total_meals = daily_meals * num_days
97
+ total_activities = daily_activities * num_days
98
+ total_transport = daily_transport * num_days
99
+
100
+ # Multiply by number of people (except accommodation which is per room)
101
+ total_meals *= num_people
102
+ total_activities *= num_people
103
+ total_transport *= num_people
104
+
105
+ # Add 10% buffer for miscellaneous expenses
106
+ buffer = (total_accommodation + total_meals + total_activities + total_transport) * 0.1
107
+
108
+ total_cost = total_accommodation + total_meals + total_activities + total_transport + buffer
109
+
110
+ return {
111
+ 'accommodation': round(total_accommodation, 2),
112
+ 'meals': round(total_meals, 2),
113
+ 'activities': round(total_activities, 2),
114
+ 'transport': round(total_transport, 2),
115
+ 'buffer': round(buffer, 2),
116
+ 'total': round(total_cost, 2)
117
+ }
118
 
119
+ def format_output(self,
120
+ destination: str,
121
+ itinerary: List[Dict],
122
+ budget: Dict) -> str:
123
+ """
124
+ Format the itinerary and budget into a readable string
125
+
126
+ Args:
127
+ destination: Name of the destination
128
+ itinerary: List of daily activities
129
+ budget: Dictionary of budget breakdown
130
+
131
+ Returns:
132
+ Formatted string with itinerary and budget
133
+ """
134
+ output = f"Travel Plan for {destination}\n\n"
135
+ output += "=== ITINERARY ===\n\n"
136
+
137
+ for day in itinerary:
138
+ output += f"Day {day['day']}:\n"
139
+ output += f"Morning: {day['morning']}\n"
140
+ output += f"Afternoon: {day['afternoon']}\n"
141
+ output += f"Evening: {day['evening']}\n"
142
+ output += f"Accommodation: {day['accommodation']}\n\n"
143
+
144
+ output += "=== BUDGET BREAKDOWN ===\n\n"
145
+ output += f"Accommodation: ${budget['accommodation']}\n"
146
+ output += f"Meals: ${budget['meals']}\n"
147
+ output += f"Activities: ${budget['activities']}\n"
148
+ output += f"Local Transport: ${budget['transport']}\n"
149
+ output += f"Buffer (10%): ${budget['buffer']}\n"
150
+ output += f"Total Estimated Cost: ${budget['total']}\n"
151
+
152
+ return output
153
 
154
+ def generate_travel_plan(destination: str,
155
+ num_days: int,
156
+ budget_level: str,
157
+ num_people: int) -> str:
158
+ """
159
+ Main function to generate complete travel plan
160
+
161
+ Args:
162
+ destination: Name of the destination
163
+ num_days: Number of days for the trip
164
+ budget_level: luxury/mid_range/budget/hostel
165
+ num_people: Number of travelers
166
+
167
+ Returns:
168
+ Formatted string with complete travel plan
169
+ """
170
+ planner = TravelPlanner()
171
+ itinerary = planner.generate_itinerary(destination, num_days, budget_level)
172
+ budget = planner.calculate_budget(num_days, budget_level, num_people)
173
+ return planner.format_output(destination, itinerary, budget)
174
 
175
+ # Integration with your Gradio interface
176
+ def respond_with_travel_plan(message: str,
177
+ history: List[Tuple[str, str]],
178
+ system_message: str,
179
+ max_tokens: int,
180
+ temperature: float,
181
+ top_p: float) -> str:
182
+ """
183
+ Modified respond function to handle travel planning requests
184
+ """
185
+ # Parse the input message for travel parameters
186
+ # This is a simple example - you might want to add more sophisticated parsing
187
+ try:
188
+ # Example message format: "Plan a trip to Paris for 5 days, mid_range budget, 2 people"
189
+ parts = message.lower().split()
190
+ destination_idx = parts.index("to") + 1
191
+ days_idx = parts.index("days") - 1
192
+ budget_idx = parts.index("budget") - 1
193
+ people_idx = parts.index("people") - 1
194
+
195
+ destination = parts[destination_idx].capitalize()
196
+ num_days = int(parts[days_idx])
197
+ budget_level = parts[budget_idx]
198
+ num_people = int(parts[people_idx])
199
+
200
+ # Generate travel plan
201
+ response = generate_travel_plan(destination, num_days, budget_level, num_people)
202
+ return response
203
+
204
+ except (ValueError, IndexError):
205
+ return "I couldn't understand your travel request. Please use the format: 'Plan a trip to [destination] for [X] days, [budget_level] budget, [X] people'"
206
 
207
+ # Update your main Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  demo = gr.ChatInterface(
209
+ respond_with_travel_plan,
210
  additional_inputs=[
211
+ gr.Textbox(value="You are a travel planning assistant.", label="System message"),
212
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
213
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
214
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
215
  ],
216
+ )