File size: 1,710 Bytes
7d71db7
173a891
3452e5e
 
 
 
 
 
 
 
7d71db7
3452e5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173a891
 
 
 
 
 
 
3452e5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import Flask, jsonify, render_template, request # Tambahkan request
from flask_cors import CORS
import os
import time
from dotenv import load_dotenv
from livekit import api

load_dotenv()

app = Flask(__name__)
CORS(app) # Tambahkan ini agar CORS aktif!

@app.route('/')
def home():
    return render_template("index.html")

@app.route('/status')
def status():
    return jsonify({
        "status": "online",
        "message": "Voice Agent API Helper is running",
        "agent_config": {
            "voice": os.getenv("INWORLD_VOICE", "default"),
            "stt": "AssemblyAI/Deepgram"
        }
    })

@app.route('/api/update-config', methods=['POST'])
def update_config():
    data = request.json
    # Di sini Anda bisa memproses data konfigurasi yang dikirim dari UI lokal
    print(f"Konfigurasi diterima: {data}")
    return jsonify({"status": "success", "message": "Konfigurasi diperbarui"})

@app.route('/token')
def get_token():
    # Ambil konfigurasi dari .env
    lk_api_key = os.getenv("LIVEKIT_API_KEY")
    lk_api_secret = os.getenv("LIVEKIT_API_SECRET")
    
    # Buat Token untuk user web
    token = api.AccessToken(lk_api_key, lk_api_secret) \
        .with_identity("web_user_" + str(int(time.time()))) \
        .with_name("Web Visitor") \
        .with_grants(api.VideoGrants(
            room_join=True,
            room="my-voice-room", # Harus sama dengan yang dipantau agent
        ))
    
    return jsonify({
        "token": token.to_jwt(),
        "server_url": os.getenv("LIVEKIT_URL")
    })

if __name__ == "__main__":
    print("Starting Flask helper on port 7860...")
    # Hugging Face Spaces menggunakan port 7860
    app.run(host="0.0.0.0", port=7860)