|
|
|
from flask import Flask, request, jsonify, send_from_directory |
|
import google.generativeai as genai |
|
from dotenv import load_dotenv |
|
import os |
|
from flask_cors import CORS |
|
import markdown2 |
|
import re |
|
from gtts import gTTS |
|
import uuid |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
AUDIO_FOLDER = os.path.join('static', 'audio') |
|
os.makedirs(AUDIO_FOLDER, exist_ok=True) |
|
|
|
app = Flask(__name__, static_folder='static') |
|
CORS(app) |
|
|
|
|
|
system_instruction = """ |
|
You are a helpful AI assistant named Athspi. When responding: |
|
1. Never mention "audio" or technical terms |
|
2. For audio responses, include content between these markers only: |
|
[AUDIO]content here[/AUDIO] |
|
3. Keep responses natural and friendly |
|
4. Always respond in a conversational tone |
|
Example good response: |
|
Here's your story! |
|
[AUDIO]Once upon a time...[/AUDIO] |
|
""" |
|
|
|
genai.configure(api_key=os.getenv("GEMINI_API_KEY")) |
|
model = genai.GenerativeModel('gemini-1.5-pro', system_instruction=system_instruction) |
|
|
|
def convert_markdown_to_html(text): |
|
html = markdown2.markdown(text, extras=["fenced-code-blocks", "tables"]) |
|
html = re.sub(r'<pre><code(.*?)>', r'<pre class="code-block"><code\1>', html) |
|
return html |
|
|
|
def process_response(full_response): |
|
"""Extract visible text and audio content""" |
|
audio_match = re.search(r'\[AUDIO\](.*?)\[/AUDIO\]', full_response, re.DOTALL) |
|
audio_content = audio_match.group(1).strip() if audio_match else None |
|
visible_text = re.sub(r'\[/?AUDIO\]', '', full_response).strip() |
|
return visible_text, audio_content |
|
|
|
def detect_audio_request(text): |
|
audio_triggers = [ |
|
'audio', 'speak', 'say it', 'read aloud', |
|
'hear', 'listen', 'tell me out loud' |
|
] |
|
return any(trigger in text.lower() for trigger in audio_triggers) |
|
|
|
def generate_audio(text): |
|
"""Generate audio file from text""" |
|
text = re.sub(r'[^\w\s.,!?\-]', '', text) |
|
filename = f"audio_{uuid.uuid4()}.mp3" |
|
filepath = os.path.join(AUDIO_FOLDER, filename) |
|
tts = gTTS(text=text, lang='en', slow=False) |
|
tts.save(filepath) |
|
return filename |
|
|
|
@app.route('/chat', methods=['POST']) |
|
def chat(): |
|
try: |
|
data = request.json |
|
user_message = data.get('message', '').strip() |
|
|
|
if not user_message: |
|
return jsonify({"error": "Message required"}), 400 |
|
|
|
audio_requested = detect_audio_request(user_message) |
|
response = model.generate_content(user_message) |
|
visible_text, audio_content = process_response(response.text) |
|
|
|
result = { |
|
"response_text": visible_text, |
|
"response_html": convert_markdown_to_html(visible_text), |
|
"has_audio": False |
|
} |
|
|
|
if audio_requested and audio_content: |
|
audio_filename = generate_audio(audio_content) |
|
result["audio_filename"] = audio_filename |
|
result["has_audio"] = True |
|
|
|
return jsonify(result) |
|
|
|
except Exception as e: |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
@app.route('/download/<filename>') |
|
def download_audio(filename): |
|
try: |
|
return send_from_directory(AUDIO_FOLDER, filename, as_attachment=True) |
|
except FileNotFoundError: |
|
return jsonify({"error": "Audio file not found"}), 404 |
|
|
|
@app.route('/') |
|
def serve_index(): |
|
return send_from_directory('static', 'index.html') |
|
|
|
@app.route('/<path:path>') |
|
def serve_static(path): |
|
return send_from_directory('static', path) |
|
|
|
if __name__ == '__main__': |
|
app.run(host="0.0.0.0", port=7860) |