Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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(
|
| 13 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 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
|
| 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 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 65 |
|
| 66 |
# --- Gradio Interface ---
|
| 67 |
-
with gr.Blocks(
|
| 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()
|
|
|