Spaces:
Runtime error
Runtime error
creating the main engine
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Form
|
2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import torch
|
5 |
+
from transformers import AutoProcessor, BarkModel, pipeline
|
6 |
+
import scipy.io.wavfile as wavfile
|
7 |
+
import uuid
|
8 |
+
import os
|
9 |
+
from typing import Optional
|
10 |
+
|
11 |
+
# Load TTS model and processor
|
12 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
13 |
+
model = BarkModel.from_pretrained("suno/bark")
|
14 |
+
|
15 |
+
# Load sentiment analysis pipeline (using multilingual model)
|
16 |
+
sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
17 |
+
|
18 |
+
# Ensure model is on CPU or CUDA if available
|
19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
+
model.to(device)
|
21 |
+
|
22 |
+
# FastAPI app
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
# Endpoint input models
|
26 |
+
class TTSRequest(BaseModel):
|
27 |
+
text: str
|
28 |
+
|
29 |
+
class SentimentRequest(BaseModel):
|
30 |
+
text: str
|
31 |
+
|
32 |
+
class LegalDocRequest(BaseModel):
|
33 |
+
text: str
|
34 |
+
domain: Optional[str] = "general"
|
35 |
+
|
36 |
+
@app.get("/")
|
37 |
+
def root():
|
38 |
+
return {"message": "Welcome to Kinyarwanda NLP API"}
|
39 |
+
|
40 |
+
@app.post("/tts/")
|
41 |
+
def text_to_speech(request: TTSRequest):
|
42 |
+
try:
|
43 |
+
# Generate speech
|
44 |
+
inputs = processor(request.text, return_tensors="pt")
|
45 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
46 |
+
speech = model.generate(**inputs)
|
47 |
+
|
48 |
+
# Save audio
|
49 |
+
output_file = f"output_{uuid.uuid4().hex}.wav"
|
50 |
+
speech_np = speech.cpu().numpy().squeeze()
|
51 |
+
wavfile.write(output_file, rate=22050, data=speech_np)
|
52 |
+
|
53 |
+
return FileResponse(output_file, media_type="audio/wav")
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
57 |
+
|
58 |
+
@app.post("/sentiment/")
|
59 |
+
def analyze_sentiment(request: SentimentRequest):
|
60 |
+
try:
|
61 |
+
result = sentiment_model(request.text)
|
62 |
+
return {"result": result}
|
63 |
+
except Exception as e:
|
64 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
65 |
+
|
66 |
+
@app.post("/legal-parse/")
|
67 |
+
def parse_legal_document(request: LegalDocRequest):
|
68 |
+
try:
|
69 |
+
# Placeholder logic (replace with training-based custom logic)
|
70 |
+
keywords = ["contract", "agreement", "party", "terms"]
|
71 |
+
found_keywords = [kw for kw in keywords if kw in request.text.lower()]
|
72 |
+
return {"identified_keywords": found_keywords, "domain": request.domain}
|
73 |
+
except Exception as e:
|
74 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|