puseletso55 commited on
Commit
f413fd3
Β·
verified Β·
1 Parent(s): 0720430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -9,8 +9,8 @@ model_id = "google/flan-t5-small"
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
  # Load tokenizer and model
12
- tokenizer = AutoTokenizer.from_pretrained("./hf-cache")
13
- model = AutoModelForSeq2SeqLM.from_pretrained("./hf-cache").to(device)
14
 
15
  # Define chatbot pipeline
16
  chatbot = pipeline("text2text-generation", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
@@ -33,25 +33,28 @@ def get_nearby_businesses(user_location):
33
  try:
34
  businesses = find_businesses_by_location(user_location)
35
  if not businesses:
36
- return "❌ No businesses found in your location."
37
  html = "<ul>"
38
  for biz in businesses:
39
- html += f"<li><b>{biz['name']}</b><br>{biz['description']}<br>πŸ“ {biz['location']} | πŸ“ž {biz['contact']}</li>"
40
  html += "</ul>"
41
  return html
42
  except Exception as e:
43
- return f"❌ Error: {str(e)}"
44
 
45
  def submit_business(name, location, description, contact):
46
  if not (name and location and contact):
47
  return "❌ Please fill in all required fields."
48
- insert_business({
49
- "name": name,
50
- "location": location,
51
- "description": description,
52
- "contact": contact
53
- })
54
- return "βœ… Business submitted successfully!"
 
 
 
55
 
56
  def get_location_name(coords):
57
  if not coords or len(coords) != 2:
@@ -59,12 +62,11 @@ def get_location_name(coords):
59
  lat, lon = coords
60
  return reverse_geocode(lat, lon)
61
 
62
- # Dummy map click function (since Gradio does not support map component directly)
63
  def update_location_from_coords(coord_str):
64
- return coord_str # Placeholder: In real app, use geolocation or map click
65
 
66
  # --- Gradio Interface ---
67
- with gr.Blocks(title="Township Chatbot with GPS") as demo:
68
  gr.Markdown("## πŸ› Township Business Chatbot Directory + GPS πŸ—ΊοΈ")
69
 
70
  location_text = gr.Textbox(label="πŸ“ Your Location", placeholder="e.g. Orange Farm, Gauteng")
@@ -89,4 +91,3 @@ with gr.Blocks(title="Township Chatbot with GPS") as demo:
89
  submit_btn.click(submit_business, [name, location_text, description, contact], submit_output)
90
 
91
  demo.launch()
92
-
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
  # Load tokenizer and model
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_id).to(device)
14
 
15
  # Define chatbot pipeline
16
  chatbot = pipeline("text2text-generation", model=model, tokenizer=tokenizer, device=0 if device == "cuda" else -1)
 
33
  try:
34
  businesses = find_businesses_by_location(user_location)
35
  if not businesses:
36
+ return "<p>❌ No businesses found in your location.</p>"
37
  html = "<ul>"
38
  for biz in businesses:
39
+ html += f"<li><b>{biz['name']}</b><br>{biz.get('description', '')}<br>πŸ“ {biz['location']} | πŸ“ž {biz['contact']}</li>"
40
  html += "</ul>"
41
  return html
42
  except Exception as e:
43
+ return f"<p>❌ Error: {str(e)}</p>"
44
 
45
  def submit_business(name, location, description, contact):
46
  if not (name and location and contact):
47
  return "❌ Please fill in all required fields."
48
+ try:
49
+ insert_business({
50
+ "name": name,
51
+ "location": location,
52
+ "description": description,
53
+ "contact": contact
54
+ })
55
+ return "βœ… Business submitted successfully!"
56
+ except Exception as e:
57
+ return f"❌ Error submitting business: {str(e)}"
58
 
59
  def get_location_name(coords):
60
  if not coords or len(coords) != 2:
 
62
  lat, lon = coords
63
  return reverse_geocode(lat, lon)
64
 
 
65
  def update_location_from_coords(coord_str):
66
+ return coord_str # Placeholder
67
 
68
  # --- Gradio Interface ---
69
+ with gr.Blocks() as demo:
70
  gr.Markdown("## πŸ› Township Business Chatbot Directory + GPS πŸ—ΊοΈ")
71
 
72
  location_text = gr.Textbox(label="πŸ“ Your Location", placeholder="e.g. Orange Farm, Gauteng")
 
91
  submit_btn.click(submit_business, [name, location_text, description, contact], submit_output)
92
 
93
  demo.launch()