|
|
|
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') |
|
if not os.path.exists(AUDIO_FOLDER): |
|
os.makedirs(AUDIO_FOLDER) |
|
|
|
|
|
app = Flask(__name__, static_folder='static') |
|
CORS(app) |
|
|
|
|
|
system_instruction_text = """ |
|
You are a helpful, friendly, and informative AI assistant named Athspi. |
|
Your goal is to provide clear, concise, and natural-sounding answers to user queries. |
|
When you respond: |
|
- Use clear and simple language. |
|
- Avoid overly complex sentence structures that might be hard to read aloud. |
|
- Keep the user engaged and offer follow-up questions or related topics where appropriate. |
|
- Ensure your responses are suitable for text-to-speech conversion. |
|
- Provide factual and accurate information. |
|
- If the user asks for audio or to speak the response, include π at the start of your response. |
|
- For coding questions, provide well-formatted code blocks with syntax highlighting. |
|
""" |
|
|
|
genai.configure(api_key=os.getenv("GEMINI_API_KEY")) |
|
model = genai.GenerativeModel( |
|
'gemini-2.5-flash', |
|
system_instruction=system_instruction_text |
|
) |
|
|
|
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) |
|
html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html) |
|
html = re.sub(r'\*(.*?)\*', r'<em>\1</em>', html) |
|
return html |
|
|
|
def detect_audio_request(text): |
|
"""Detect if user is requesting audio""" |
|
audio_keywords = ['audio', 'speak', 'say it', 'read aloud', 'hear', 'listen'] |
|
return any(keyword in text.lower() for keyword in audio_keywords) |
|
|
|
def generate_audio_file(text): |
|
"""Generate audio file from text and return filename""" |
|
cleaned_text = re.sub(r'[\*_`#]', '', text) |
|
cleaned_text = re.sub(r'\s+', ' ', cleaned_text).strip() |
|
|
|
if not cleaned_text: |
|
return None |
|
|
|
filename = f"{uuid.uuid4()}.mp3" |
|
filepath = os.path.join(AUDIO_FOLDER, filename) |
|
|
|
tts = gTTS(text=cleaned_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') |
|
|
|
if not user_message: |
|
return jsonify({"error": "No message provided"}), 400 |
|
|
|
|
|
audio_requested = detect_audio_request(user_message) |
|
|
|
response = model.generate_content(user_message) |
|
plain_text_response = response.text |
|
|
|
|
|
audio_url = None |
|
if audio_requested: |
|
|
|
if not plain_text_response.startswith("π"): |
|
plain_text_response = "π " + plain_text_response |
|
|
|
|
|
audio_filename = generate_audio_file(plain_text_response) |
|
if audio_filename: |
|
audio_url = f"/static/audio/{audio_filename}" |
|
|
|
html_response = convert_markdown_to_html(plain_text_response) |
|
|
|
return jsonify({ |
|
"response_html": html_response, |
|
"response_text": plain_text_response, |
|
"audio_url": audio_url |
|
}) |
|
|
|
except Exception as e: |
|
app.logger.error(f"Chat Error: {e}") |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
@app.route('/generate-audio', methods=['POST']) |
|
def generate_audio(): |
|
try: |
|
data = request.json |
|
text_to_speak = data.get('text') |
|
|
|
if not text_to_speak: |
|
return jsonify({"error": "No text provided"}), 400 |
|
|
|
cleaned_text = re.sub(r'[\*_`#]', '', text_to_speak) |
|
cleaned_text = re.sub(r'\s+', ' ', cleaned_text).strip() |
|
|
|
if not cleaned_text: |
|
return jsonify({"error": "Text became empty after cleaning"}), 400 |
|
|
|
filename = f"{uuid.uuid4()}.mp3" |
|
filepath = os.path.join(AUDIO_FOLDER, filename) |
|
|
|
tts = gTTS(text=cleaned_text, lang='en', slow=False) |
|
tts.save(filepath) |
|
|
|
audio_url = f"/static/audio/{filename}" |
|
return jsonify({"audio_url": audio_url}) |
|
|
|
except Exception as e: |
|
app.logger.error(f"Audio Generation Error: {e}") |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
@app.route('/static/audio/<filename>') |
|
def serve_audio(filename): |
|
return send_from_directory(AUDIO_FOLDER, filename) |
|
|
|
@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) |