Atualizar app/app.py
This commit is contained in:
+55
@@ -341,8 +341,63 @@ def search():
|
||||
db.close()
|
||||
return jsonify(results)
|
||||
|
||||
from pywhispercpp.model import Model
|
||||
@app.route("/api/audio/transcribe/<cid>", methods=["POST"])
|
||||
@login_required
|
||||
@require_capability("can_use_audio")
|
||||
def transcribe(cid):
|
||||
# Initialize the model (automatically downloads 'base.en' if not present)
|
||||
model = Model('base.en', print_realtime=False, print_progress=False)
|
||||
|
||||
|
||||
file = request.files.get("file")
|
||||
if not file:
|
||||
return jsonify({"error": "No audio file"}), 400
|
||||
|
||||
path = f"/data/audio-{uuid.uuid4()}.wav"
|
||||
file.save(path)
|
||||
|
||||
# Example using whisper (pseudo-code)
|
||||
# result = whisper.transcribe(path)
|
||||
# text = result["text"]
|
||||
|
||||
# Transcribe your audio file (must be 16kHz WAV format)
|
||||
segments = model.transcribe(path)
|
||||
|
||||
# Print the text results
|
||||
for segment in segments:
|
||||
print(f"[{segment.t0} -> {segment.t1}]: {segment.text}")
|
||||
|
||||
text = "Transcribed text placeholder"
|
||||
|
||||
db = SessionLocal()
|
||||
msg = Message(
|
||||
id=str(uuid.uuid4()),
|
||||
conversation_id=cid,
|
||||
role="user",
|
||||
content=text,
|
||||
)
|
||||
db.add(msg)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
return jsonify({"status": "ok", "text": text})
|
||||
|
||||
from gtts import gTTS
|
||||
|
||||
@app.route("/api/audio/synthesize/<cid>", methods=["POST"])
|
||||
@login_required
|
||||
@require_capability("can_use_audio")
|
||||
def synthesize(cid):
|
||||
text = request.json.get("text", "")
|
||||
if not text:
|
||||
return jsonify({"error": "No text"}), 400
|
||||
|
||||
tts = gTTS(text=text, lang="en")
|
||||
path = f"/data/tts-{uuid.uuid4()}.mp3"
|
||||
tts.save(path)
|
||||
|
||||
return send_file(path, mimetype="audio/mpeg", as_attachment=False)
|
||||
|
||||
|
||||
# ***
|
||||
|
||||
Reference in New Issue
Block a user