Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 4 |
+
import uvicorn
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# --- CONFIGURATION ---
|
| 11 |
+
# 🚨 POINTING TO YOUR REBRANDED MODEL
|
| 12 |
+
MODEL_ID = "metanthropic/neural-voice-v1"
|
| 13 |
+
device = "cpu" # Free tier is CPU-only
|
| 14 |
+
torch_dtype = torch.float32
|
| 15 |
+
|
| 16 |
+
print(f"🔹 Loading Sovereign Model: {MODEL_ID}...")
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# 1. Load Model
|
| 20 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
torch_dtype=torch_dtype,
|
| 23 |
+
low_cpu_mem_usage=True,
|
| 24 |
+
use_safetensors=True
|
| 25 |
+
)
|
| 26 |
+
model.to(device)
|
| 27 |
+
|
| 28 |
+
# 2. Load Processor
|
| 29 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 30 |
+
|
| 31 |
+
# 3. Create Pipeline
|
| 32 |
+
pipe = pipeline(
|
| 33 |
+
"automatic-speech-recognition",
|
| 34 |
+
model=model,
|
| 35 |
+
tokenizer=processor.tokenizer,
|
| 36 |
+
feature_extractor=processor.feature_extractor,
|
| 37 |
+
max_new_tokens=128,
|
| 38 |
+
chunk_length_s=15,
|
| 39 |
+
batch_size=16,
|
| 40 |
+
torch_dtype=torch_dtype,
|
| 41 |
+
device=device,
|
| 42 |
+
)
|
| 43 |
+
print("✅ Model Loaded Successfully.")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"❌ Error loading model: {e}")
|
| 46 |
+
raise e
|
| 47 |
+
|
| 48 |
+
@app.get("/")
|
| 49 |
+
def home():
|
| 50 |
+
return {"status": "Metanthropic Neural Voice Node Online", "model": MODEL_ID}
|
| 51 |
+
|
| 52 |
+
@app.post("/transcribe")
|
| 53 |
+
async def transcribe(file: UploadFile = File(...)):
|
| 54 |
+
# Create a temporary file to store the upload
|
| 55 |
+
temp_filename = f"temp_{file.filename}"
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
# Save uploaded file
|
| 59 |
+
with open(temp_filename, "wb") as buffer:
|
| 60 |
+
shutil.copyfileobj(file.file, buffer)
|
| 61 |
+
|
| 62 |
+
# Run Inference (The Magic)
|
| 63 |
+
print(f"🎙️ Transcribing {temp_filename}...")
|
| 64 |
+
result = pipe(temp_filename)
|
| 65 |
+
text = result["text"]
|
| 66 |
+
|
| 67 |
+
return {"text": text.strip()}
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"❌ Transcription Error: {e}")
|
| 71 |
+
return {"error": str(e), "text": ""} # Return empty text on error
|
| 72 |
+
|
| 73 |
+
finally:
|
| 74 |
+
# Cleanup temp file
|
| 75 |
+
if os.path.exists(temp_filename):
|
| 76 |
+
os.remove(temp_filename)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|