Ravishankarsharma commited on
Commit
22021b0
·
verified ·
1 Parent(s): 2108c71

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import tempfile
4
+ import requests
5
+ from fastapi import FastAPI, UploadFile, File, HTTPException
6
+ from fastapi.responses import JSONResponse, HTMLResponse
7
+ from pydantic import BaseModel
8
+ from gradio_client import Client, handle_file
9
+ import uvicorn
10
+
11
+ app = FastAPI(title="Audio Transcription API (via URL)")
12
+
13
+ # Hugging Face client
14
+ try:
15
+ client = Client("Ravishankarsharma/voice2text-summarizer")
16
+ except Exception as e:
17
+ print("Warning: Hugging Face client failed:", e)
18
+ client = None
19
+
20
+ # ✅ Pydantic model for URL input
21
+ class AudioURL(BaseModel):
22
+ url: str
23
+
24
+ @app.get("/", response_class=HTMLResponse)
25
+ async def home():
26
+ return HTMLResponse("""
27
+ <html><body>
28
+ <h2>Submit an audio URL to transcribe:</h2>
29
+ <form action="/transcribe_url" method="post">
30
+ <input name="url" type="text" placeholder="Enter audio URL" required>
31
+ <button type="submit">Transcribe</button>
32
+ </form>
33
+ <p>Swagger: <a href="/docs">/docs</a></p>
34
+ </body></html>
35
+ """)
36
+
37
+ # ✅ New endpoint: Accepts URL instead of file
38
+ @app.post("/transcribe_url")
39
+ async def transcribe_from_url(audio: AudioURL):
40
+ if not client:
41
+ raise HTTPException(status_code=500, detail="Hugging Face client not initialized")
42
+
43
+ try:
44
+ # 1. Download the audio file from given URL
45
+ response = requests.get(audio.url, stream=True)
46
+ if response.status_code != 200:
47
+ raise HTTPException(status_code=400, detail="Failed to download audio file from URL")
48
+
49
+ # 2. Save it temporarily
50
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
51
+ for chunk in response.iter_content(chunk_size=8192):
52
+ tmp.write(chunk)
53
+ tmp_path = tmp.name
54
+
55
+ # 3. Send to Hugging Face model
56
+ result = client.predict(handle_file(tmp_path), api_name="/predict")
57
+ os.remove(tmp_path)
58
+
59
+ return {
60
+ "source_url": audio.url,
61
+ "transcription": result[0],
62
+ "summary": result[1],
63
+ "api_endpoint": result[2]
64
+ }
65
+ except Exception as e:
66
+ raise HTTPException(status_code=500, detail=str(e))
67
+
68
+
69
+ if __name__ == "__main__":
70
+ print("Server running at http://127.0.0.1:8000")
71
+ uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)