Chatbot-3 / app.py
ShabanEjupi's picture
Update app.py
3c42841 verified
import gradio as gr
import requests
import os
import time
from threading import Thread, Event
# Configuration
COLAB_URL = os.getenv("COLAB_URL", "https://7ab1-35-187-228-56.ngrok-free.app/chat")
TIMEOUT = 25
POLL_INTERVAL = 3 # Seconds between status checks
# Connection manager
class ConnectionManager:
def __init__(self):
self.active = False
self.stop_event = Event()
self.thread = Thread(target=self._monitor, daemon=True)
self.thread.start()
def _monitor(self):
while not self.stop_event.is_set():
try:
response = requests.get(f"{COLAB_URL}/health", timeout=5)
self.active = response.status_code == 200
except:
self.active = False
time.sleep(POLL_INTERVAL)
def stop(self):
self.stop_event.set()
conn = ConnectionManager()
def respond(message, history):
if not conn.active:
return "πŸ”΄ Backend offline - keep Colab notebook running"
try:
response = requests.post(
f"{COLAB_URL}/chat",
json={"prompt": message},
headers={"Content-Type": "application/json"},
timeout=TIMEOUT
)
return response.json().get("response", "Empty response")
except requests.exceptions.RequestException as e:
return f"⚠️ Connection error: {str(e)}"
# Create interface
with gr.Blocks(title="Phi-3 Chatbot") as demo:
gr.Markdown("## πŸ’¬ Phi-3-mini (Colab Backend)")
status = gr.Textbox(
value="🟒 Connected" if conn.active else "πŸ”΄ Connecting...",
label="Status",
interactive=False
)
# Manual status updates
def update_status():
return "🟒 Connected" if conn.active else "πŸ”΄ Backend offline"
# Add dummy hidden component for polling
dummy = gr.Textbox(visible=False)
# Set up polling
def poll_status():
while True:
time.sleep(POLL_INTERVAL)
yield update_status()
dummy.change(
fn=poll_status,
outputs=status,
show_progress=False
)
chatbot = gr.Chatbot(height=350)
msg = gr.Textbox(placeholder="Type message...")
msg.submit(respond, [msg, chatbot], [msg, chatbot])
# Launch with cleanup
try:
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
finally:
conn.stop()