burak commited on
Commit
4b5595b
1 Parent(s): 7bf22df

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +228 -0
app.py ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ PlaceRecommender
2
+ @author: Burak Kaya
3
+ @email: [email protected]
4
+
5
+ """
6
+
7
+ import gradio as gr
8
+ from huggingface_hub import InferenceClient
9
+ import json
10
+ from openai import OpenAI
11
+ import os
12
+ import requests
13
+ import googlemaps
14
+ import pandas as pd
15
+
16
+ OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
17
+ MAPS_API_KEY = os.environ.get('MAPS_API_KEY')
18
+
19
+ client = OpenAI(api_key=OPENAI_API_KEY)
20
+
21
+ def get_place_details(place_id, api_key):
22
+ URL = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={api_key}"
23
+ response = requests.get(URL)
24
+ if response.status_code == 200:
25
+ result = json.loads(response.content)["result"]
26
+ return result
27
+ else:
28
+ print(f"Google Place Details API request failed with status code {response.status_code}")
29
+ print(f"Response content: {response.content}")
30
+ return None
31
+
32
+
33
+ def generate_short_answer(user_input):
34
+ OpenAI(api_key=OPENAI_API_KEY)
35
+
36
+
37
+ response = client.chat.completions.create(
38
+ model="gpt-3.5-turbo-0125",
39
+ messages=[
40
+ {
41
+ "role": "system",
42
+ "content": "Kullanıcı girdisine dayanarak, cümlenin ana konusunu veya amacını temsil eden bir arama kelimesi veya kelime öbeği çıkarın. Herhangi bir ek metin veya açıklama olmadan yalnızca arama sözcüğünü veya ifadesini çıktı olarak sağlayın."
43
+ },
44
+ {
45
+ "role": "user",
46
+ "content": user_input
47
+ }
48
+ ],
49
+ temperature=0,
50
+ max_tokens=256,
51
+ top_p=1,
52
+ frequency_penalty=0,
53
+ presence_penalty=0
54
+ )
55
+
56
+ output_string = response.choices[0].message.content
57
+ return output_string
58
+
59
+
60
+ def call_google_places_api(location, food_preference=None):
61
+ try:
62
+ map_client = googlemaps.Client(MAPS_API_KEY)
63
+
64
+ search_string = food_preference
65
+ address = location
66
+ geocode = map_client.geocode(address=address)
67
+ (lat, lng) = map(geocode[0]['geometry']['location'].get, ('lat', 'lng'))
68
+
69
+ response = map_client.places_nearby(
70
+ location=(lat, lng),
71
+ keyword=search_string,
72
+ radius=500
73
+ )
74
+
75
+ business_list = response.get('results')
76
+
77
+ df = pd.DataFrame(business_list)
78
+ df['url'] = 'https://www.google.com/maps/place/?q=place_id:' + df['place_id']
79
+
80
+ top_places = df[(df['user_ratings_total'] > 100) & (df['rating'] >= 4.2)].sort_values(by=['rating', 'user_ratings_total']).sort_values(by=['rating','user_ratings_total'],ascending=False).head(4)
81
+
82
+ places = []
83
+ for _, place in top_places.iterrows():
84
+ place_id = place['place_id']
85
+ place_details = get_place_details(place_id, MAPS_API_KEY)
86
+ place_name = place_details.get("name", "N/A")
87
+ place_rating = place_details.get("rating", "N/A")
88
+ total_ratings = place_details.get("user_ratings_total", "N/A")
89
+ place_address = place_details.get("vicinity", "N/A")
90
+ place_url = place_details.get("url", "N/A")
91
+ place_reviews = []
92
+
93
+ reviews = place_details.get("reviews", [])
94
+ for review in reviews[:5]:
95
+ review_dict = {
96
+ "text": review["text"],
97
+ }
98
+ place_reviews.append(review_dict)
99
+
100
+
101
+ if ',' in place_address:
102
+ street_address = place_address.split(',')[0]
103
+ else:
104
+ street_address = place_address
105
+
106
+ place_info = f"[{place_name}]({place_url}) is a located at {street_address}. It has a rating of {place_rating} based on {total_ratings} user reviews: {place_reviews}. \n"
107
+ places.append(place_info)
108
+
109
+ return places
110
+ except Exception as e:
111
+ print(f"Error during the Google Places API call with radius 1000: {e}")
112
+ try:
113
+ response = map_client.places_nearby(
114
+ location=(lat, lng),
115
+ keyword=search_string,
116
+ radius=1500
117
+ )
118
+
119
+ business_list = response.get('results')
120
+
121
+ df = pd.DataFrame(business_list)
122
+ df['url'] = 'https://www.google.com/maps/place/?q=place_id:' + df['place_id']
123
+
124
+ top_places = df[(df['user_ratings_total'] > 100) & (df['rating'] >= 4.2)].sort_values(by=['rating', 'user_ratings_total']).sort_values(by=['rating','user_ratings_total'],ascending=False).head(4)
125
+
126
+ places = []
127
+ for _, place in top_places.iterrows():
128
+ place_id = place['place_id']
129
+ place_details = get_place_details(place_id, MAPS_API_KEY)
130
+ place_name = place_details.get("name", "N/A")
131
+ place_rating = place_details.get("rating", "N/A")
132
+ total_ratings = place_details.get("user_ratings_total", "N/A")
133
+ place_address = place_details.get("vicinity", "N/A")
134
+ place_url = place_details.get("url", "N/A")
135
+ place_reviews = []
136
+
137
+ reviews = place_details.get("reviews", [])
138
+ for review in reviews[:5]:
139
+ review_dict = {
140
+ "text": review["text"],
141
+ }
142
+ place_reviews.append(review_dict)
143
+
144
+
145
+ if ',' in place_address:
146
+ street_address = place_address.split(',')[0]
147
+ else:
148
+ street_address = place_address
149
+
150
+ place_info = f"[{place_name}]({place_url}) is a located at {street_address}. It has a rating of {place_rating} based on {total_ratings} user reviews: {place_reviews}. \n"
151
+ places.append(place_info)
152
+
153
+ return places
154
+ except Exception as e:
155
+ print(f"Error during the Google Places API call with radius 2000: {e}")
156
+ return []
157
+
158
+ def provide_user_specific_recommendations(user_input, location):
159
+ place_type = generate_short_answer(user_input)
160
+ places = call_google_places_api(location, place_type)
161
+ if places:
162
+ return f"Here are some places you might be interested in: {' '.join(places)}"
163
+ else:
164
+ return "Yakınlarda ilgi çekici bir yer bulamadım. Lütfen başka bir şekilde dile getirmeyi deneyiniz"
165
+
166
+
167
+ def bot(user_input, history,Konum):
168
+
169
+ if Konum:
170
+ output = provide_user_specific_recommendations(user_input, Konum)
171
+
172
+ OpenAI(api_key=OPENAI_API_KEY)
173
+
174
+
175
+ stream = client.chat.completions.create(
176
+ model="gpt-3.5-turbo-0125",
177
+ messages=[
178
+ {
179
+ "role": "system",
180
+ "content": "Sen bir mekan tavsiyecisisin. Kullanicinin sorusuna uygun mekanlari buluyorsun, mekan yorumlarini anlayarak o mekanlari neden secmesi gerektigini tek tek ozetliyorsun.**[cafe ismi](url link)** formatini muhakkak ver. Emoji kullanmaktan cekinme"
181
+ },
182
+ {
183
+ "role": "user",
184
+ "content": user_input + output
185
+ }
186
+ ],
187
+ temperature=0,
188
+ max_tokens=1024,
189
+ top_p=1,
190
+ frequency_penalty=0,
191
+ presence_penalty=0,
192
+ stream=True
193
+ )
194
+ #streaming
195
+ partial_message = ""
196
+ for chunk in stream:
197
+ if chunk.choices[0].delta.content is not None:
198
+ partial_message = partial_message + chunk.choices[0].delta.content
199
+ yield partial_message
200
+
201
+ yield partial_message
202
+
203
+
204
+
205
+ my_theme = gr.Theme.from_hub("bethecloud/storj_theme")
206
+
207
+ with gr.Blocks(theme = my_theme) as demo:
208
+ #gr.HTML("<h1><center>MÜDAVİM<h1><center>")
209
+ with gr.Row() as Konum:
210
+ Konum = gr.Textbox(label="Konum",placeholder="Konum Giriniz (Kadıköy, Moda)")
211
+
212
+ chatbot = gr.Chatbot(
213
+ [],
214
+ elem_id="chatbot",
215
+ bubble_full_width=False,
216
+ render=False,
217
+ height=500,
218
+ )
219
+
220
+
221
+ gr.ChatInterface(
222
+ bot,chatbot=chatbot,
223
+ additional_inputs=Konum,
224
+ examples=[["Arkadașlarımla oturup kahve içebileceğimiz bir yer arıyoruz. Önerebileceğin yerler var mı?"], ["Bilgisayar ile çalışabileceğim sessiz kafe var mı?"], ["Kız arkadaşım ile özel bir akşam yemeği yemek istiyoruz. Bir tavsiyen var mı?"]],)
225
+
226
+
227
+ if __name__ == "__main__":
228
+ demo.launch(show_api=False)