Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py - Gradio wrapper for
|
2 |
import os
|
3 |
import shutil
|
4 |
import asyncio
|
@@ -6,14 +6,15 @@ import json
|
|
6 |
from typing import Optional
|
7 |
|
8 |
import gradio as gr
|
|
|
9 |
from multimodal_module import MultiModalChatModule
|
10 |
|
11 |
# Instantiate AI
|
12 |
AI = MultiModalChatModule()
|
13 |
|
14 |
-
#
|
15 |
-
# File wrapper
|
16 |
-
#
|
17 |
class GradioFileWrapper:
|
18 |
def __init__(self, gr_file):
|
19 |
if isinstance(gr_file, str):
|
@@ -31,12 +32,23 @@ class GradioFileWrapper:
|
|
31 |
loop = asyncio.get_event_loop()
|
32 |
await loop.run_in_executor(None, shutil.copyfile, self._path, dst_path)
|
33 |
|
|
|
|
|
|
|
|
|
34 |
def run_async(coro):
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
#
|
39 |
-
#
|
|
|
40 |
def text_chat(user_id: Optional[int], text: str, lang: str = "en"):
|
41 |
try:
|
42 |
uid = int(user_id) if user_id else 0
|
@@ -45,6 +57,7 @@ def text_chat(user_id: Optional[int], text: str, lang: str = "en"):
|
|
45 |
except Exception as e:
|
46 |
return f"Error: {e}"
|
47 |
|
|
|
48 |
def voice_process(user_id: Optional[int], audio_file):
|
49 |
try:
|
50 |
uid = int(user_id) if user_id else 0
|
@@ -54,6 +67,7 @@ def voice_process(user_id: Optional[int], audio_file):
|
|
54 |
except Exception as e:
|
55 |
return f"Error: {e}"
|
56 |
|
|
|
57 |
def generate_voice(user_id: Optional[int], reply_text: str, fmt: str = "ogg"):
|
58 |
try:
|
59 |
uid = int(user_id) if user_id else 0
|
@@ -62,6 +76,7 @@ def generate_voice(user_id: Optional[int], reply_text: str, fmt: str = "ogg"):
|
|
62 |
except Exception as e:
|
63 |
return None, f"Error: {e}"
|
64 |
|
|
|
65 |
def image_caption(user_id: Optional[int], image_file):
|
66 |
try:
|
67 |
uid = int(user_id) if user_id else 0
|
@@ -71,6 +86,7 @@ def image_caption(user_id: Optional[int], image_file):
|
|
71 |
except Exception as e:
|
72 |
return f"Error: {e}"
|
73 |
|
|
|
74 |
def generate_image(user_id: Optional[int], prompt: str, width: int = 512, height: int = 512, steps: int = 30):
|
75 |
try:
|
76 |
uid = int(user_id) if user_id else 0
|
@@ -79,6 +95,7 @@ def generate_image(user_id: Optional[int], prompt: str, width: int = 512, height
|
|
79 |
except Exception as e:
|
80 |
return f"Error: {e}"
|
81 |
|
|
|
82 |
def edit_image(user_id: Optional[int], image_file, mask_file, prompt: str = ""):
|
83 |
try:
|
84 |
uid = int(user_id) if user_id else 0
|
@@ -89,6 +106,7 @@ def edit_image(user_id: Optional[int], image_file, mask_file, prompt: str = ""):
|
|
89 |
except Exception as e:
|
90 |
return f"Error: {e}"
|
91 |
|
|
|
92 |
def process_video(user_id: Optional[int], video_file):
|
93 |
try:
|
94 |
uid = int(user_id) if user_id else 0
|
@@ -98,6 +116,7 @@ def process_video(user_id: Optional[int], video_file):
|
|
98 |
except Exception as e:
|
99 |
return f"Error: {e}"
|
100 |
|
|
|
101 |
def process_file(user_id: Optional[int], file_obj):
|
102 |
try:
|
103 |
uid = int(user_id) if user_id else 0
|
@@ -107,6 +126,7 @@ def process_file(user_id: Optional[int], file_obj):
|
|
107 |
except Exception as e:
|
108 |
return f"Error: {e}"
|
109 |
|
|
|
110 |
def code_complete(user_id: Optional[int], prompt: str, max_tokens: int = 512):
|
111 |
try:
|
112 |
uid = int(user_id) if user_id else 0
|
@@ -115,9 +135,28 @@ def code_complete(user_id: Optional[int], prompt: str, max_tokens: int = 512):
|
|
115 |
except Exception as e:
|
116 |
return f"Error: {e}"
|
117 |
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
# Gradio UI
|
120 |
-
#
|
121 |
with gr.Blocks(title="Multimodal Bot (Gradio)") as demo:
|
122 |
gr.Markdown("# 🧠 Multimodal Bot\nInteract via text, voice, images, video, or files.")
|
123 |
|
@@ -185,5 +224,19 @@ with gr.Blocks(title="Multimodal Bot (Gradio)") as demo:
|
|
185 |
|
186 |
gr.Markdown("----\nThis Space runs your exact `multimodal_module.py`. First requests may take longer due to model loading.")
|
187 |
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
1 |
+
# app.py - Hybrid Gradio + FastAPI wrapper for multimodal_module.py
|
2 |
import os
|
3 |
import shutil
|
4 |
import asyncio
|
|
|
6 |
from typing import Optional
|
7 |
|
8 |
import gradio as gr
|
9 |
+
from fastapi import FastAPI, Request
|
10 |
from multimodal_module import MultiModalChatModule
|
11 |
|
12 |
# Instantiate AI
|
13 |
AI = MultiModalChatModule()
|
14 |
|
15 |
+
# ============================================================
|
16 |
+
# Helper: File wrapper for Gradio uploads
|
17 |
+
# ============================================================
|
18 |
class GradioFileWrapper:
|
19 |
def __init__(self, gr_file):
|
20 |
if isinstance(gr_file, str):
|
|
|
32 |
loop = asyncio.get_event_loop()
|
33 |
await loop.run_in_executor(None, shutil.copyfile, self._path, dst_path)
|
34 |
|
35 |
+
|
36 |
+
# ============================================================
|
37 |
+
# Async-safe helper
|
38 |
+
# ============================================================
|
39 |
def run_async(coro):
|
40 |
+
try:
|
41 |
+
loop = asyncio.get_running_loop()
|
42 |
+
except RuntimeError:
|
43 |
+
loop = asyncio.new_event_loop()
|
44 |
+
asyncio.set_event_loop(loop)
|
45 |
+
loop = asyncio.get_event_loop()
|
46 |
+
return loop.run_until_complete(coro)
|
47 |
|
48 |
+
|
49 |
+
# ============================================================
|
50 |
+
# Callback functions (used by Gradio & API)
|
51 |
+
# ============================================================
|
52 |
def text_chat(user_id: Optional[int], text: str, lang: str = "en"):
|
53 |
try:
|
54 |
uid = int(user_id) if user_id else 0
|
|
|
57 |
except Exception as e:
|
58 |
return f"Error: {e}"
|
59 |
|
60 |
+
|
61 |
def voice_process(user_id: Optional[int], audio_file):
|
62 |
try:
|
63 |
uid = int(user_id) if user_id else 0
|
|
|
67 |
except Exception as e:
|
68 |
return f"Error: {e}"
|
69 |
|
70 |
+
|
71 |
def generate_voice(user_id: Optional[int], reply_text: str, fmt: str = "ogg"):
|
72 |
try:
|
73 |
uid = int(user_id) if user_id else 0
|
|
|
76 |
except Exception as e:
|
77 |
return None, f"Error: {e}"
|
78 |
|
79 |
+
|
80 |
def image_caption(user_id: Optional[int], image_file):
|
81 |
try:
|
82 |
uid = int(user_id) if user_id else 0
|
|
|
86 |
except Exception as e:
|
87 |
return f"Error: {e}"
|
88 |
|
89 |
+
|
90 |
def generate_image(user_id: Optional[int], prompt: str, width: int = 512, height: int = 512, steps: int = 30):
|
91 |
try:
|
92 |
uid = int(user_id) if user_id else 0
|
|
|
95 |
except Exception as e:
|
96 |
return f"Error: {e}"
|
97 |
|
98 |
+
|
99 |
def edit_image(user_id: Optional[int], image_file, mask_file, prompt: str = ""):
|
100 |
try:
|
101 |
uid = int(user_id) if user_id else 0
|
|
|
106 |
except Exception as e:
|
107 |
return f"Error: {e}"
|
108 |
|
109 |
+
|
110 |
def process_video(user_id: Optional[int], video_file):
|
111 |
try:
|
112 |
uid = int(user_id) if user_id else 0
|
|
|
116 |
except Exception as e:
|
117 |
return f"Error: {e}"
|
118 |
|
119 |
+
|
120 |
def process_file(user_id: Optional[int], file_obj):
|
121 |
try:
|
122 |
uid = int(user_id) if user_id else 0
|
|
|
126 |
except Exception as e:
|
127 |
return f"Error: {e}"
|
128 |
|
129 |
+
|
130 |
def code_complete(user_id: Optional[int], prompt: str, max_tokens: int = 512):
|
131 |
try:
|
132 |
uid = int(user_id) if user_id else 0
|
|
|
135 |
except Exception as e:
|
136 |
return f"Error: {e}"
|
137 |
|
138 |
+
|
139 |
+
# ============================================================
|
140 |
+
# FastAPI public API
|
141 |
+
# ============================================================
|
142 |
+
api = FastAPI()
|
143 |
+
|
144 |
+
@api.post("/api/predict")
|
145 |
+
async def api_predict(request: Request):
|
146 |
+
try:
|
147 |
+
data = await request.json()
|
148 |
+
user_id = data.get("user_id", 0)
|
149 |
+
text = data.get("text", "")
|
150 |
+
lang = data.get("lang", "en")
|
151 |
+
reply = text_chat(user_id, text, lang)
|
152 |
+
return {"status": "ok", "reply": reply}
|
153 |
+
except Exception as e:
|
154 |
+
return {"status": "error", "message": str(e)}
|
155 |
+
|
156 |
+
|
157 |
+
# ============================================================
|
158 |
# Gradio UI
|
159 |
+
# ============================================================
|
160 |
with gr.Blocks(title="Multimodal Bot (Gradio)") as demo:
|
161 |
gr.Markdown("# 🧠 Multimodal Bot\nInteract via text, voice, images, video, or files.")
|
162 |
|
|
|
224 |
|
225 |
gr.Markdown("----\nThis Space runs your exact `multimodal_module.py`. First requests may take longer due to model loading.")
|
226 |
|
227 |
+
|
228 |
+
# ============================================================
|
229 |
+
# Launch both API + Gradio
|
230 |
+
# ============================================================
|
231 |
+
import uvicorn
|
232 |
+
from threading import Thread
|
233 |
+
|
234 |
+
def start_api():
|
235 |
+
uvicorn.run(api, host="0.0.0.0", port=8000)
|
236 |
+
|
237 |
+
# Start FastAPI in a separate thread
|
238 |
+
Thread(target=start_api, daemon=True).start()
|
239 |
+
|
240 |
+
# Launch Gradio
|
241 |
+
demo.queue()
|
242 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|