Spaces:
Build error
Build error
| import os, json, requests, threading | |
| from flask import Flask, request, jsonify | |
| from groq_transcribe import groq_transcribe | |
| from labelstudio_inject import upload_prediction | |
| from correction_collector import save_correction | |
| from dataset_collector import prepare_dataset | |
| app = Flask(__name__) | |
| # Load env | |
| LABEL_URL = os.environ.get("LABEL_STUDIO_URL") | |
| API_TOKEN = os.environ.get("LABEL_STUDIO_API_TOKEN") | |
| HEADERS = {"Authorization": f"Token {API_TOKEN}", "Content-Type": "application/json"} | |
| def get_latest_project_id(): | |
| """Dynamic Project ID fetch""" | |
| r = requests.get(f"{LABEL_URL}/api/projects/", headers=HEADERS) | |
| r.raise_for_status() | |
| projects = r.json() | |
| latest_project = sorted(projects, key=lambda x: x['id'], reverse=True)[0] | |
| return latest_project['id'] | |
| PROJECT_ID = get_latest_project_id() | |
| # ----------------------------- | |
| # Routes | |
| # ----------------------------- | |
| def ml_health(): | |
| return jsonify({ | |
| "engine": "groq", | |
| "label_studio_url": LABEL_URL, | |
| "language": os.environ.get("WHISPER_LANGUAGE"), | |
| "model_version": os.environ.get("GROQ_MODEL"), | |
| "project_id": PROJECT_ID, | |
| "status": "ok" | |
| }) | |
| def auto_transcribe(): | |
| """Upload audio and auto transcribe using Groq, inject to Label Studio""" | |
| audio_file = request.files.get("audio") | |
| if not audio_file: | |
| return jsonify({"error": "No audio file uploaded"}), 400 | |
| audio_path = f"/tmp/{audio_file.filename}" | |
| audio_file.save(audio_path) | |
| # Transcription in background thread | |
| def transcribe_task(path): | |
| try: | |
| transcript = groq_transcribe(path) | |
| # For demo, pick first task in project | |
| task_id = request.form.get("task_id") | |
| if not task_id: | |
| # fallback: get latest task from Label Studio | |
| r = requests.get(f"{LABEL_URL}/api/projects/{PROJECT_ID}/tasks/", headers=HEADERS) | |
| r.raise_for_status() | |
| tasks = r.json() | |
| if tasks: | |
| task_id = tasks[0]['id'] | |
| if task_id: | |
| upload_prediction(task_id, transcript) | |
| print(f"Transcribed {path} → Label Studio task {task_id}") | |
| except Exception as e: | |
| print("Error in transcription:", e) | |
| threading.Thread(target=transcribe_task, args=(audio_path,)).start() | |
| return jsonify({"status": "started"}) | |
| def save_correction_route(): | |
| """Human corrected transcription""" | |
| data = request.json | |
| task_id = data.get("task_id") | |
| human_text = data.get("human_text") | |
| groq_text = data.get("groq_text") | |
| audio_path = data.get("audio_path") | |
| if not all([task_id, human_text, groq_text, audio_path]): | |
| return jsonify({"error": "Missing field"}), 400 | |
| save_correction(task_id, human_text, groq_text, audio_path) | |
| return jsonify({"status": "saved"}) | |
| def prepare_dataset_route(): | |
| """Prepare dataset for fine-tune""" | |
| prepare_dataset() | |
| return jsonify({"status": "dataset_ready"}) | |
| # ----------------------------- | |
| # Run App | |
| # ----------------------------- | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 7860)) | |
| app.run(host="0.0.0.0", port=port) |