Ali Abdullah
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import mimetypes
|
| 4 |
import os
|
| 5 |
-
import threading
|
| 6 |
-
import uvicorn
|
| 7 |
import re
|
| 8 |
|
| 9 |
# === Fix surrogate character issue ===
|
|
@@ -13,13 +11,7 @@ def sanitize(text):
|
|
| 13 |
return text.encode("utf-8", "ignore").decode("utf-8")
|
| 14 |
return text
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def run_fastapi():
|
| 18 |
-
uvicorn.run("main:app", host="0.0.0.0", port=8000)
|
| 19 |
-
|
| 20 |
-
threading.Thread(target=run_fastapi, daemon=True).start()
|
| 21 |
-
|
| 22 |
-
# Backend API endpoints
|
| 23 |
FILE_API_URL = "http://127.0.0.1:8000/chat-with-file"
|
| 24 |
URL_API_URL = "http://127.0.0.1:8000/chat-with-url"
|
| 25 |
IMAGE_API_URL = "http://127.0.0.1:8000/extract-text-from-image"
|
|
@@ -33,7 +25,6 @@ def format_chat(history):
|
|
| 33 |
def ask_from_file(file_obj, question, session_id):
|
| 34 |
if not file_obj or not question.strip():
|
| 35 |
return "⚠️ Please upload a file and enter a question.", chat_sessions["file"]
|
| 36 |
-
|
| 37 |
if os.path.getsize(file_obj.name) > 10 * 1024 * 1024:
|
| 38 |
return "❌ File too large. Please upload files under 10MB.", chat_sessions["file"]
|
| 39 |
|
|
@@ -90,6 +81,7 @@ def clear_url_chat():
|
|
| 90 |
chat_sessions["url"] = []
|
| 91 |
return "", chat_sessions["url"]
|
| 92 |
|
|
|
|
| 93 |
custom_css = """
|
| 94 |
body, .gradio-container {
|
| 95 |
background-color: #111111 !important;
|
|
@@ -129,6 +121,7 @@ h1 {
|
|
| 129 |
}
|
| 130 |
"""
|
| 131 |
|
|
|
|
| 132 |
with gr.Blocks(css=custom_css) as demo:
|
| 133 |
gr.Markdown("# 🤖 AI Chatbot with File, Web, Image, Audio & Chat History")
|
| 134 |
|
|
|
|
| 2 |
import requests
|
| 3 |
import mimetypes
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
# === Fix surrogate character issue ===
|
|
|
|
| 11 |
return text.encode("utf-8", "ignore").decode("utf-8")
|
| 12 |
return text
|
| 13 |
|
| 14 |
+
# API endpoints (your FastAPI runs in main.py separately)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
FILE_API_URL = "http://127.0.0.1:8000/chat-with-file"
|
| 16 |
URL_API_URL = "http://127.0.0.1:8000/chat-with-url"
|
| 17 |
IMAGE_API_URL = "http://127.0.0.1:8000/extract-text-from-image"
|
|
|
|
| 25 |
def ask_from_file(file_obj, question, session_id):
|
| 26 |
if not file_obj or not question.strip():
|
| 27 |
return "⚠️ Please upload a file and enter a question.", chat_sessions["file"]
|
|
|
|
| 28 |
if os.path.getsize(file_obj.name) > 10 * 1024 * 1024:
|
| 29 |
return "❌ File too large. Please upload files under 10MB.", chat_sessions["file"]
|
| 30 |
|
|
|
|
| 81 |
chat_sessions["url"] = []
|
| 82 |
return "", chat_sessions["url"]
|
| 83 |
|
| 84 |
+
# 💅 CSS
|
| 85 |
custom_css = """
|
| 86 |
body, .gradio-container {
|
| 87 |
background-color: #111111 !important;
|
|
|
|
| 121 |
}
|
| 122 |
"""
|
| 123 |
|
| 124 |
+
# UI
|
| 125 |
with gr.Blocks(css=custom_css) as demo:
|
| 126 |
gr.Markdown("# 🤖 AI Chatbot with File, Web, Image, Audio & Chat History")
|
| 127 |
|