Prince03Raval commited on
Commit
10425b2
·
verified ·
1 Parent(s): 4685278
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -2,11 +2,12 @@ import gradio as gr
2
  from transformers import pipeline
3
  from fastapi import FastAPI, Request
4
  from fastapi.responses import JSONResponse
 
5
 
6
  # ===== Load the model =====
7
  model = pipeline("text-generation", model="microsoft/DialoGPT-medium")
8
 
9
- # ===== Define chatbot logic =====
10
  def chat(message):
11
  if not message.strip():
12
  return "⚠️ Please enter a message."
@@ -14,7 +15,7 @@ def chat(message):
14
  reply = result[len(message):].strip()
15
  return reply or "🤖 Sorry, I didn’t quite catch that."
16
 
17
- # ===== Gradio Interface =====
18
  demo = gr.Interface(
19
  fn=chat,
20
  inputs=gr.Textbox(label="Message"),
@@ -23,9 +24,10 @@ demo = gr.Interface(
23
  description="Smart, adaptive chatbot powered by Hugging Face"
24
  )
25
 
26
- # ===== FastAPI backend to expose /api/predict =====
27
- app = FastAPI()
28
 
 
29
  @app.post("/api/predict/")
30
  async def predict(request: Request):
31
  try:
@@ -36,11 +38,9 @@ async def predict(request: Request):
36
  except Exception as e:
37
  return JSONResponse({"error": str(e)}, status_code=500)
38
 
39
- # ===== Mount Gradio app =====
40
- gr_app = gr.routes.App.create_app(demo)
41
- app.mount("/", gr_app)
42
 
43
- # ===== Launch manually (only for local testing) =====
44
  if __name__ == "__main__":
45
- demo.queue()
46
- demo.launch(show_api=False)
 
2
  from transformers import pipeline
3
  from fastapi import FastAPI, Request
4
  from fastapi.responses import JSONResponse
5
+ import uvicorn
6
 
7
  # ===== Load the model =====
8
  model = pipeline("text-generation", model="microsoft/DialoGPT-medium")
9
 
10
+ # ===== Chat function =====
11
  def chat(message):
12
  if not message.strip():
13
  return "⚠️ Please enter a message."
 
15
  reply = result[len(message):].strip()
16
  return reply or "🤖 Sorry, I didn’t quite catch that."
17
 
18
+ # ===== Gradio app =====
19
  demo = gr.Interface(
20
  fn=chat,
21
  inputs=gr.Textbox(label="Message"),
 
24
  description="Smart, adaptive chatbot powered by Hugging Face"
25
  )
26
 
27
+ # ===== Create FastAPI app =====
28
+ app = FastAPI(title="Savenzer AI API", version="2.0")
29
 
30
+ # ===== Add custom /api/predict route =====
31
  @app.post("/api/predict/")
32
  async def predict(request: Request):
33
  try:
 
38
  except Exception as e:
39
  return JSONResponse({"error": str(e)}, status_code=500)
40
 
41
+ # ===== Mount Gradio UI to root =====
42
+ app = gr.mount_gradio_app(app, demo, path="/")
 
43
 
44
+ # ===== Local run only (not needed on Hugging Face) =====
45
  if __name__ == "__main__":
46
+ uvicorn.run(app, host="0.0.0.0", port=7860)