Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import whisper
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
| 7 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# ================== LOAD MODELS =====================
|
| 10 |
# Load Whisper (Speech-to-Text)
|
| 11 |
whisper_model = whisper.load_model("base")
|
|
@@ -36,10 +42,14 @@ def transcribe_and_summarize(audio_path):
|
|
| 36 |
except Exception as e:
|
| 37 |
summary = f"Summarization failed: {e}"
|
| 38 |
|
| 39 |
-
# Save
|
| 40 |
-
os.
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# API URL (replace with your deployed Hugging Face Space URL)
|
| 45 |
api_link = "https://your-username-your-space-name.hf.space/run/predict"
|
|
@@ -64,12 +74,10 @@ api_app = FastAPI(title="Audio Transcriber + Summarizer API")
|
|
| 64 |
|
| 65 |
@api_app.post("/api/transcribe/")
|
| 66 |
async def transcribe_api(file: UploadFile = File(...)):
|
| 67 |
-
|
| 68 |
-
temp_path = f"/tmp/{file.filename}"
|
| 69 |
with open(temp_path, "wb") as f:
|
| 70 |
f.write(await file.read())
|
| 71 |
|
| 72 |
-
# Call the same function as Gradio
|
| 73 |
transcription, summary, api_link = transcribe_and_summarize(temp_path)
|
| 74 |
return {"transcription": transcription, "summary": summary, "api_url": api_link}
|
| 75 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import csv
|
| 3 |
import whisper
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
| 7 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 8 |
|
| 9 |
+
# ================== DIRECTORIES =====================
|
| 10 |
+
UPLOAD_DIR = "./uploads"
|
| 11 |
+
CSV_DIR = "./csv_output"
|
| 12 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 13 |
+
os.makedirs(CSV_DIR, exist_ok=True)
|
| 14 |
+
|
| 15 |
# ================== LOAD MODELS =====================
|
| 16 |
# Load Whisper (Speech-to-Text)
|
| 17 |
whisper_model = whisper.load_model("base")
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
summary = f"Summarization failed: {e}"
|
| 44 |
|
| 45 |
+
# Save to CSV
|
| 46 |
+
csv_file = os.path.join(CSV_DIR, "transcription_summary.csv")
|
| 47 |
+
file_exists = os.path.isfile(csv_file)
|
| 48 |
+
with open(csv_file, mode="a", newline="", encoding="utf-8") as f:
|
| 49 |
+
writer = csv.writer(f)
|
| 50 |
+
if not file_exists:
|
| 51 |
+
writer.writerow(["Audio File", "Transcription", "Summary"])
|
| 52 |
+
writer.writerow([os.path.basename(audio_path), transcription, summary])
|
| 53 |
|
| 54 |
# API URL (replace with your deployed Hugging Face Space URL)
|
| 55 |
api_link = "https://your-username-your-space-name.hf.space/run/predict"
|
|
|
|
| 74 |
|
| 75 |
@api_app.post("/api/transcribe/")
|
| 76 |
async def transcribe_api(file: UploadFile = File(...)):
|
| 77 |
+
temp_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 78 |
with open(temp_path, "wb") as f:
|
| 79 |
f.write(await file.read())
|
| 80 |
|
|
|
|
| 81 |
transcription, summary, api_link = transcribe_and_summarize(temp_path)
|
| 82 |
return {"transcription": transcription, "summary": summary, "api_url": api_link}
|
| 83 |
|