Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import tempfile
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
from fastapi.responses import HTMLResponse
|
| 7 |
+
from gradio_client import Client
|
| 8 |
+
import uvicorn
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="Meeting Summarizer API")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Replace this with your actual deployed HF space/model if needed
|
| 15 |
+
HF_MODEL_SPACE = "Ravishankarsharma/voice2text-summarizer"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Public demo audio (replace if you want your own)
|
| 19 |
+
DEM0_AUDIO_URL = "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/0001.flac"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Initialize HF client
|
| 23 |
+
try:
|
| 24 |
+
client = Client(HF_MODEL_SPACE)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print("⚠️ Client initialization failed:", e)
|
| 27 |
+
client = None
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.get("/", response_class=HTMLResponse)
|
| 33 |
+
async def home():
|
| 34 |
+
return """
|
| 35 |
+
<html><body>
|
| 36 |
+
<h2>Meeting Summarizer API</h2>
|
| 37 |
+
<p>➡️ Call <a href='/summarize'>/summarize</a> to get meeting summary</p>
|
| 38 |
+
<p>➡️ Swagger docs: <a href='/docs'>/docs</a></p>
|
| 39 |
+
</body></html>
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@app.get("/summarize")
|
| 46 |
+
async def summarize_meeting():
|
| 47 |
+
if not client:
|
| 48 |
+
raise HTTPException(status_code=500, detail="❌ Hugging Face client not initialized")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
meeting_url = DEM0_AUDIO_URL
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Download audio
|
| 56 |
+
response = requests.get(meeting_url, stream=True, timeout=30)
|
| 57 |
+
if response.status_code != 200:
|
| 58 |
+
raise HTTPException(status_code=400, detail=f"Failed to fetch meeting audio (status {response.status_code})")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
suffix = ".flac" if meeting_url.endswith('.flac') else os.path.splitext(meeting_url)[1] or ".wav"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
| 65 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 66 |
+
if chunk:
|
| 67 |
+
tmp.write(chunk)
|
| 68 |
+
tmp_path = tmp.name
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# Call your HF model. The predict input depends on how your space expects input.
|
| 72 |
+
# If your space expects a file, use handle_file(tmp_path) instead. Here we try both common ways.
|
| 73 |
+
try:
|
| 74 |
+
# First try: send file path (many gradio-based spaces accept this)
|
| 75 |
+
result = client.predict(tmp_path, api_name="/predict")
|
| 76 |
+
except Exception:
|
| 77 |
+
# Fallback: send the raw bytes
|
| 78 |
+
with open(tmp_path, "rb") as fd:
|
| 79 |
+
data = fd.read()
|
| 80 |
+
result = client.predict(data, api_name="/predict")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Clean up
|
| 84 |
+
try:
|
| 85 |
+
os.remove(tmp_path)
|
| 86 |
+
except Exception:
|
| 87 |
+
pass
|
| 88 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|