AsyncCoder-10 commited on
Commit
c4c9802
·
verified ·
1 Parent(s): 66432ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -16
app.py CHANGED
@@ -1,16 +1,16 @@
1
- import uvicorn
2
  import gradio as gr
3
  import spaces
4
- from fastapi import FastAPI
5
 
6
  # Import your FastAPI app and logic
7
- from app.main import app as original_fastapi_app, moderate_text, TextRequest
8
 
 
9
  @spaces.GPU
10
  def _satisfy_zerogpu_startup_check():
11
  return "GPU available"
12
 
13
- # The UI function
14
  async def moderate_ui(text):
15
  if not text.strip():
16
  return "Please enter some text."
@@ -32,7 +32,7 @@ async def moderate_ui(text):
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
35
- # Create Gradio UI
36
  demo = gr.Interface(
37
  fn=moderate_ui,
38
  inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
@@ -40,16 +40,9 @@ demo = gr.Interface(
40
  title="Multilingual Moderation API",
41
  )
42
 
43
- # ✅ THE FIX: Access Gradio's internal FastAPI app and attach your routes
44
- app = demo.app
45
-
46
- # Re-add your FastAPI routes to the app that Hugging Face is actually serving
47
- app.include_router(original_fastapi_app.router)
48
 
49
- # Launch Gradio (this properly initializes @spaces.GPU)
50
  if __name__ == "__main__":
51
- demo.launch(
52
- server_name="0.0.0.0",
53
- server_port=7860,
54
- share=False
55
- )
 
 
1
  import gradio as gr
2
  import spaces
3
+ import uvicorn
4
 
5
  # Import your FastAPI app and logic
6
+ from app.main import app as fastapi_app, moderate_text, TextRequest
7
 
8
+ # 1. Dummy function to pass ZeroGPU check
9
  @spaces.GPU
10
  def _satisfy_zerogpu_startup_check():
11
  return "GPU available"
12
 
13
+ # 2. The UI function
14
  async def moderate_ui(text):
15
  if not text.strip():
16
  return "Please enter some text."
 
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
35
+ # 3. Create Gradio UI
36
  demo = gr.Interface(
37
  fn=moderate_ui,
38
  inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
 
40
  title="Multilingual Moderation API",
41
  )
42
 
43
+ # 4. THE CRITICAL FIX: Mount Gradio at "/ui" and let FastAPI own the rest
44
+ app = gr.mount_gradio_app(fastapi_app, demo, path="/ui")
 
 
 
45
 
 
46
  if __name__ == "__main__":
47
+ # Run the combined app
48
+ uvicorn.run(app, host="0.0.0.0", port=7860)