| import gradio as gr |
| import pymongo |
| import whisper |
| from geopy.distance import geodesic |
| import json |
| import subprocess |
|
|
| |
| whisper_model = whisper.load_model("base") |
|
|
| |
| mongo_client = pymongo.MongoClient( |
| "mongodb+srv://township_chatbot:Daniel%409615@cluster0.x0stlrp.mongodb.net/db?retryWrites=true&w=majority&appName=Cluster0" |
| ) |
| db = mongo_client["township_db"] |
| businesses = db["businesses"] |
|
|
| def find_nearby(lat, lon, radius=10): |
| user_loc = (float(lat), float(lon)) |
| nearby = [] |
| for b in businesses.find({}): |
| try: |
| dist = geodesic(user_loc, (float(b["latitude"]), float(b["longitude"]))).km |
| if dist <= radius: |
| b["distance_km"] = round(dist, 2) |
| b["_id"] = str(b["_id"]) |
| nearby.append(b) |
| except Exception: |
| continue |
| return json.dumps(nearby, indent=2) |
|
|
| def voice_chat(audio_file): |
| if audio_file is None: |
| return "Please say something..." |
| transcription = whisper_model.transcribe(audio_file)["text"] |
| return f"You said: {transcription}" |
|
|
| def chat_with_ollama(user_message, chat_history): |
| if chat_history is None: |
| chat_history = [] |
| prompt = "" |
| for u, b in chat_history: |
| prompt += f"User: {u}\nAssistant: {b}\n" |
| prompt += f"User: {user_message}\nAssistant:" |
|
|
| try: |
| result = subprocess.run( |
| ["ollama", "run", "township_business_growth_coach", prompt], |
| capture_output=True, |
| text=True, |
| timeout=30 |
| ) |
| reply = result.stdout.strip() if result.returncode == 0 else "Error: " + result.stderr.strip() |
| except Exception as e: |
| reply = f"Exception: {str(e)}" |
|
|
| chat_history.append((user_message, reply)) |
| return chat_history, chat_history |
|
|
| with gr.Blocks(title="Township Connect Chatbot") as demo: |
|
|
| gr.Markdown(""" |
| <div style='text-align: center; font-size: 28px; font-weight: bold; color: #333;'>ποΈ Township Connect Chatbot</div> |
| <div style='text-align: center; font-size: 16px; color: #666;'>Helping local businesses grow with AI</div> |
| <hr/> |
| """) |
|
|
| |
| gr.Markdown("## π Find Nearby Businesses") |
| with gr.Row(): |
| lat = gr.Number(label="Latitude", value=-26.2041) |
| lon = gr.Number(label="Longitude", value=28.0473) |
| radius = gr.Slider(1, 50, value=5, label="Radius (km)") |
| map_result = gr.Textbox(label="Nearby Businesses", lines=10) |
| search_button = gr.Button("π Search Businesses") |
| search_button.click(find_nearby, [lat, lon, radius], map_result) |
|
|
| |
| gr.Markdown("## π€ Voice-to-Text") |
| audio_input = gr.Audio(type="filepath") |
| audio_output = gr.Textbox(label="Transcription") |
| transcribe_button = gr.Button("Transcribe") |
| transcribe_button.click(voice_chat, inputs=audio_input, outputs=audio_output) |
|
|
| |
| gr.Markdown("## π¬ Township Growth Chatbot") |
| chatbot = gr.Chatbot(label="Chat History") |
| msg = gr.Textbox(placeholder="Ask your question here...") |
| state = gr.State([]) |
|
|
| msg.submit(chat_with_ollama, [msg, state], [chatbot, state]) |
| send_button = gr.Button("Send") |
| send_button.click(chat_with_ollama, [msg, state], [chatbot, state]) |
|
|
| |
| demo.launch() |
|
|
|
|