mahesh1209 commited on
Commit
72fdea6
Β·
verified Β·
1 Parent(s): defb8ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -4,19 +4,20 @@ from pydantic import BaseModel
4
  from prometheus_client import Counter, generate_latest, CONTENT_TYPE_LATEST
5
  from transformers import pipeline
6
  import gradio as gr
 
7
 
8
- # βœ… Expose FastAPI app at top level
9
  app = FastAPI()
10
 
11
  # πŸ“Š Prometheus metrics
12
  triage_requests = Counter("triage_requests_total", "Total triage requests")
13
  triage_errors = Counter("triage_errors_total", "Total triage errors")
14
 
15
- # 🧠 Load lightweight model (CPU only)
16
  triage_pipeline = pipeline(
17
  "text2text-generation",
18
- model="google/flan-t5-small",
19
- device=-1 # Force CPU
20
  )
21
 
22
  # πŸ“₯ Request model
@@ -29,7 +30,7 @@ async def triage(incident: Incident):
29
  triage_requests.inc()
30
  try:
31
  prompt = f"Classify this incident and suggest priority:\n\n{incident.description}"
32
- result = triage_pipeline(prompt, max_new_tokens=100)[0]["generated_text"]
33
  return {"triage": result.strip()}
34
  except Exception as e:
35
  triage_errors.inc()
@@ -50,7 +51,7 @@ def gradio_triage(description):
50
  try:
51
  triage_requests.inc()
52
  prompt = f"Classify this incident and suggest priority:\n\n{description}"
53
- result = triage_pipeline(prompt, max_new_tokens=100)[0]["generated_text"]
54
  return result.strip()
55
  except Exception as e:
56
  triage_errors.inc()
@@ -61,9 +62,9 @@ demo = gr.Interface(
61
  inputs=gr.Textbox(lines=5, placeholder="Describe the incident..."),
62
  outputs="text",
63
  title="Incident Triage Bot",
64
- description="Classify incidents and suggest priority using a lightweight Transformers model"
65
  )
66
 
67
- @app.get("/gradio")
68
- def gradio_ui():
69
- return Response(content=demo.launch(share=False, inline=True), media_type="text/html")
 
4
  from prometheus_client import Counter, generate_latest, CONTENT_TYPE_LATEST
5
  from transformers import pipeline
6
  import gradio as gr
7
+ import uvicorn
8
 
9
+ # βœ… FastAPI app
10
  app = FastAPI()
11
 
12
  # πŸ“Š Prometheus metrics
13
  triage_requests = Counter("triage_requests_total", "Total triage requests")
14
  triage_errors = Counter("triage_errors_total", "Total triage errors")
15
 
16
+ # 🧠 Load ultra-lightweight model
17
  triage_pipeline = pipeline(
18
  "text2text-generation",
19
+ model="sshleifer/tiny-t5", # ~25MB model
20
+ device=-1
21
  )
22
 
23
  # πŸ“₯ Request model
 
30
  triage_requests.inc()
31
  try:
32
  prompt = f"Classify this incident and suggest priority:\n\n{incident.description}"
33
+ result = triage_pipeline(prompt, max_new_tokens=50)[0]["generated_text"]
34
  return {"triage": result.strip()}
35
  except Exception as e:
36
  triage_errors.inc()
 
51
  try:
52
  triage_requests.inc()
53
  prompt = f"Classify this incident and suggest priority:\n\n{description}"
54
+ result = triage_pipeline(prompt, max_new_tokens=50)[0]["generated_text"]
55
  return result.strip()
56
  except Exception as e:
57
  triage_errors.inc()
 
62
  inputs=gr.Textbox(lines=5, placeholder="Describe the incident..."),
63
  outputs="text",
64
  title="Incident Triage Bot",
65
+ description="Classify incidents and suggest priority using a tiny Transformers model"
66
  )
67
 
68
+ # βœ… Mount Gradio properly
69
+ from fastapi.middleware.wsgi import WSGIMiddleware
70
+ app.mount("/gradio", gr.mount_gradio_app(app, demo, path="/gradio"))