yahya1912 commited on
Commit
476a41c
·
verified ·
1 Parent(s): ff61ae6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -3,6 +3,8 @@ from huggingface_hub import InferenceClient
3
  import json, re, os, requests
4
  import csv
5
  from io import StringIO
 
 
6
 
7
  # API Keys from Space settings
8
  hf_token = os.getenv("HF_TOKEN")
@@ -66,6 +68,7 @@ def build_search_query(parsed_data):
66
 
67
  return " ".join(parts)
68
 
 
69
  def search_google_maps_new_api(parsed_data):
70
  """استخدام Places API (New) v1"""
71
  if not google_key:
@@ -225,6 +228,7 @@ def search_google_maps_legacy(parsed_data, query=None):
225
  except Exception as e:
226
  return f"❌ فشل في Legacy API أيضاً: {str(e)}"
227
 
 
228
  def parse_arabic_query(user_query):
229
  """تحليل الاستعلام باستخدام LLM"""
230
  try:
@@ -235,7 +239,7 @@ def parse_arabic_query(user_query):
235
 
236
  response = client.chat_completion(
237
  messages=messages,
238
- max_tokens=300,
239
  temperature=0.1,
240
  stream=False
241
  )
@@ -258,17 +262,24 @@ def parse_arabic_query(user_query):
258
  return {"error": f"خطأ في التحليل: {str(e)}"}
259
 
260
  def main_process(user_query):
 
261
  if not user_query.strip():
262
  return {"error": "الرجاء إدخال استعلام"}, "لا توجد نتائج"
263
 
264
  parsed_data = parse_arabic_query(user_query)
265
 
266
  if "error" in parsed_data:
267
- return parsed_data, f"⚠️ {parsed_data.get('error', 'خطأ غير معروف')}"
268
 
269
  map_results = search_google_maps_new_api(parsed_data)
270
 
271
- return parsed_data, map_results
 
 
 
 
 
 
272
 
273
  def process_batch_queries(file_obj, progress=gr.Progress()):
274
  if file_obj is None:
@@ -334,6 +345,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
334
  **يدعم جميع الفئات:** مطاعم، فنادق، مستشفيات، محطات وقود، حدائق، متاجر، وغيرها...
335
 
336
  ⚠️ **تأكد من تفعيل Places API (New) في Google Cloud Console**
 
 
337
  """)
338
 
339
  with gr.Tab("استعلام فردي"):
@@ -390,4 +403,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
390
  outputs=[batch_output_file, batch_status_message]
391
  )
392
 
393
- demo.launch()
 
3
  import json, re, os, requests
4
  import csv
5
  from io import StringIO
6
+ import functools
7
+ import time
8
 
9
  # API Keys from Space settings
10
  hf_token = os.getenv("HF_TOKEN")
 
68
 
69
  return " ".join(parts)
70
 
71
+ @functools.lru_cache(maxsize=128)
72
  def search_google_maps_new_api(parsed_data):
73
  """استخدام Places API (New) v1"""
74
  if not google_key:
 
228
  except Exception as e:
229
  return f"❌ فشل في Legacy API أيضاً: {str(e)}"
230
 
231
+ @functools.lru_cache(maxsize=128)
232
  def parse_arabic_query(user_query):
233
  """تحليل الاستعلام باستخدام LLM"""
234
  try:
 
239
 
240
  response = client.chat_completion(
241
  messages=messages,
242
+ max_tokens=100, # Reduced for faster response, adjust if needed for complex queries
243
  temperature=0.1,
244
  stream=False
245
  )
 
262
  return {"error": f"خطأ في التحليل: {str(e)}"}
263
 
264
  def main_process(user_query):
265
+ start_time = time.perf_counter()
266
  if not user_query.strip():
267
  return {"error": "الرجاء إدخال استعلام"}, "لا توجد نتائج"
268
 
269
  parsed_data = parse_arabic_query(user_query)
270
 
271
  if "error" in parsed_data:
272
+ return parsed_data, f"⚠️ {parsed_data.get("error", "خطأ غير معروف")}"
273
 
274
  map_results = search_google_maps_new_api(parsed_data)
275
 
276
+ end_time = time.perf_counter()
277
+ response_time_ms = (end_time - start_time) * 1000
278
+
279
+ # Append response time to map_results for display
280
+ map_results_with_time = f"⏱️ وقت الاستجابة: {response_time_ms:.2f} مللي ثانية\n\n" + map_results
281
+
282
+ return parsed_data, map_results_with_time
283
 
284
  def process_batch_queries(file_obj, progress=gr.Progress()):
285
  if file_obj is None:
 
345
  **يدعم جميع الفئات:** مطاعم، فنادق، مستشفيات، محطات وقود، حدائق، متاجر، وغيرها...
346
 
347
  ⚠️ **تأكد من تفعيل Places API (New) في Google Cloud Console**
348
+
349
+ _ملاحظة: تحقيق زمن استجابة أقل من 100 مللي ثانية صعب للغاية بسبب زمن استجابة الشبكة لخدمات LLM و Google Maps الخارجية. تم تطبيق التحسينات القصوى، ولكن زمن الاستجابة سيعتمد بشكل كبير على أداء هذه الخدمات._
350
  """)
351
 
352
  with gr.Tab("استعلام فردي"):
 
403
  outputs=[batch_output_file, batch_status_message]
404
  )
405
 
406
+ demo.launch()