Princeaka commited on
Commit
2688b9e
·
verified ·
1 Parent(s): 720a169

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -18
app.py CHANGED
@@ -1,15 +1,13 @@
1
- # app.py — Full multimodal FastAPI + Gradio with auto free port pick
2
  import os
3
  import shutil
4
  import asyncio
5
  import inspect
6
- import socket
7
  from typing import Optional
8
  from fastapi import FastAPI, UploadFile, File, Form
9
  from fastapi.responses import JSONResponse
10
  from fastapi.middleware.cors import CORSMiddleware
11
  import gradio as gr
12
- import uvicorn
13
 
14
  from multimodal_module import MultiModalChatModule
15
  AI = MultiModalChatModule()
@@ -46,14 +44,13 @@ app = FastAPI(title="Multimodal Module API")
46
 
47
  app.add_middleware(
48
  CORSMiddleware,
49
- allow_origins=["*"], # Change this in production!
50
  allow_credentials=True,
51
  allow_methods=["*"],
52
  allow_headers=["*"],
53
  )
54
 
55
  # --- API Endpoints ---
56
-
57
  @app.post("/api/text")
58
  async def api_text(text: str = Form(...), user_id: Optional[int] = Form(0), lang: str = Form("en")):
59
  try:
@@ -180,16 +177,5 @@ with gr.Blocks(title="Multimodal Bot (UI)") as demo:
180
  out = gr.Textbox(lines=6, label="Reply")
181
  gr.Button("Send").click(gradio_text_fn, [inp, uid, lang], out)
182
 
183
- app = gr.mount_gradio_app(app, demo, path="/")
184
-
185
- # --- Auto free port finder ---
186
- def get_free_port():
187
- s = socket.socket()
188
- s.bind(("", 0))
189
- port = s.getsockname()[1]
190
- s.close()
191
- return port
192
-
193
- if __name__ == "__main__":
194
- port = int(os.environ.get("PORT", get_free_port()))
195
- uvicorn.run("app:app", host="0.0.0.0", port=port)
 
1
+ # app.py — Full multimodal FastAPI + Gradio (Hugging Face Spaces compatible)
2
  import os
3
  import shutil
4
  import asyncio
5
  import inspect
 
6
  from typing import Optional
7
  from fastapi import FastAPI, UploadFile, File, Form
8
  from fastapi.responses import JSONResponse
9
  from fastapi.middleware.cors import CORSMiddleware
10
  import gradio as gr
 
11
 
12
  from multimodal_module import MultiModalChatModule
13
  AI = MultiModalChatModule()
 
44
 
45
  app.add_middleware(
46
  CORSMiddleware,
47
+ allow_origins=["*"], # Change in production!
48
  allow_credentials=True,
49
  allow_methods=["*"],
50
  allow_headers=["*"],
51
  )
52
 
53
  # --- API Endpoints ---
 
54
  @app.post("/api/text")
55
  async def api_text(text: str = Form(...), user_id: Optional[int] = Form(0), lang: str = Form("en")):
56
  try:
 
177
  out = gr.Textbox(lines=6, label="Reply")
178
  gr.Button("Send").click(gradio_text_fn, [inp, uid, lang], out)
179
 
180
+ # Mount Gradio to FastAPI
181
+ app = gr.mount_gradio_app(app, demo, path="/")