Guest User

backend

a guest
May 15th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. from flask import Flask, render_template, request, jsonify
  2. import requests
  3. import os
  4. import soundfile as sf
  5. import numpy as np
  6. from kokoro import KPipeline
  7. import uuid
  8. import base64
  9.  
  10. app = Flask(__name__)
  11.  
  12. # Ollama endpoint configuration
  13. OLLAMA_API_URL = "http://localhost:11434/api/generate"
  14. OLLAMA_TAGS_URL = "http://localhost:11434/api/tags"
  15.  
  16. # Kokoro TTS configuration (latest version 1.0)
  17. KOKORO_MODEL_PATH = "models/kokoro-v1.0.onnx"
  18. KOKORO_VOICES_PATH = "models/voices-v1.0.bin"
  19. KOKORO_LANG_CODE = "a" # American English
  20. VOICE = "af_bella" # Default voice
  21.  
  22. # Initialize Kokoro TTS pipeline
  23. try:
  24. tts_pipeline = KPipeline(lang_code=KOKORO_LANG_CODE)
  25. except Exception as e:
  26. print(f"Error initializing Kokoro TTS: {str(e)}")
  27. tts_pipeline = None
  28.  
  29. # Ensure model and voice files exist
  30. MODEL_DIR = "models"
  31. os.makedirs(MODEL_DIR, exist_ok=True)
  32.  
  33. def download_kokoro_model():
  34. """Download the latest Kokoro model and voices if not present."""
  35. if not os.path.exists(KOKORO_MODEL_PATH):
  36. try:
  37. model_url = "https://github.com/nazdridoy/kokoro-tts/releases/download/v1.0.0/kokoro-v1.0.onnx"
  38. voices_url = "https://github.com/nazdridoy/kokoro-tts/releases/download/v1.0.0/voices-v1.0.bin"
  39. os.makedirs(MODEL_DIR, exist_ok=True)
  40. for url, path in [(model_url, KOKORO_MODEL_PATH), (voices_url, KOKORO_VOICES_PATH)]:
  41. print(f"Downloading {url} to {path}")
  42. response = requests.get(url)
  43. response.raise_for_status()
  44. with open(path, "wb") as f:
  45. f.write(response.content)
  46. except Exception as e:
  47. print(f"Error downloading Kokoro model: {str(e)}")
  48.  
  49. download_kokoro_model()
  50.  
  51. def get_ollama_models():
  52. """Fetch available models from Ollama API."""
  53. try:
  54. response = requests.get(OLLAMA_TAGS_URL)
  55. response.raise_for_status()
  56. models_data = response.json().get("models", [])
  57. return [model["name"] for model in models_data]
  58. except requests.RequestException as e:
  59. print(f"Error fetching Ollama models: {str(e)}")
  60. return ["llama2"] # Fallback model
  61.  
  62. @app.route('/')
  63. def index():
  64. models = get_ollama_models()
  65. return render_template('index2.html', models=models)
  66.  
  67. @app.route('/process_text', methods=['POST'])
  68. def process_text():
  69. data = request.json
  70. user_input = data.get('text')
  71. selected_model = data.get('model')
  72. system_prompt = data.get('system_prompt', "You are a helpful, friendly AI assistant.")
  73.  
  74. if not user_input:
  75. return jsonify({"error": "No input provided"}), 400
  76.  
  77. # Validate selected model
  78. available_models = get_ollama_models()
  79. if selected_model not in available_models:
  80. selected_model = available_models[0] if available_models else "llama2"
  81.  
  82. # Send to Ollama with the system prompt
  83. try:
  84. ollama_response = requests.post(
  85. OLLAMA_API_URL,
  86. json={
  87. "model": selected_model,
  88. "prompt": user_input,
  89. "system": system_prompt, # Add the system prompt here
  90. "stream": False
  91. }
  92. )
  93. ollama_response.raise_for_status()
  94. response_text = ollama_response.json().get("response", "")
  95. except requests.RequestException as e:
  96. return jsonify({"error": f"Ollama API error: {str(e)}"}), 500
  97.  
  98. # Generate audio with Kokoro TTS
  99. audio_data = None
  100. if tts_pipeline:
  101. try:
  102. generator = tts_pipeline(response_text, voice=VOICE)
  103. audio_chunks = []
  104. for _, _, audio in generator:
  105. audio_chunks.append(audio)
  106. final_audio = np.concatenate(audio_chunks)
  107.  
  108. # Save audio to temporary file
  109. temp_audio_path = f"static/output_{uuid.uuid4()}.wav"
  110. sf.write(temp_audio_path, final_audio, 24000)
  111.  
  112. # Convert audio to base64 for web playback
  113. with open(temp_audio_path, "rb") as audio_file:
  114. audio_data = base64.b64encode(audio_file.read()).decode('utf-8')
  115.  
  116. # Clean up
  117. os.remove(temp_audio_path)
  118. except Exception as e:
  119. return jsonify({"error": f"TTS generation error: {str(e)}"}), 500
  120.  
  121. return jsonify({
  122. "text": response_text,
  123. "audio": audio_data
  124. })
  125.  
  126. if __name__ == '__main__':
  127. app.run(host='0.0.0.0', port=5000)
Add Comment
Please, Sign In to add comment