| import os |
| import json |
| import time |
| import io |
| import threading |
| import uuid |
| import requests |
| import re |
| import logging |
| import random |
| import base64 |
| import sqlite3 |
| import concurrent.futures |
| from datetime import datetime, timedelta |
| from itertools import cycle |
| from flask import Flask, request, jsonify, render_template, send_file |
| from flask_cors import CORS |
| from pydub import AudioSegment |
|
|
| |
| CACHE_DIRECTORY = "/tmp/huggingface_cache_ezmary" |
| os.makedirs(CACHE_DIRECTORY, exist_ok=True) |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') |
|
|
| app = Flask(__name__) |
| CORS(app) |
|
|
| |
| WORKER_URLS = [ |
| "https://opera8-ttspro.hf.space/generate", |
| "https://opera8-ttspro1.hf.space/generate", |
| "https://hamed744-ttspro.hf.space/generate", |
| "https://hamed744-ttspro2.hf.space/generate", |
| ] |
|
|
| |
| VC_SPACE_URL = "https://ezmarynoori-sada.hf.space" |
|
|
| |
| tasks = {} |
| tasks_lock = threading.Lock() |
| USAGE_LIMIT = 5 |
|
|
| |
| STANDARD_HEADERS = { |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", |
| "Accept": "application/json", |
| "Content-Type": "application/json" |
| } |
|
|
| |
| DB_DIR = "/data" if os.path.isdir("/data") else "." |
| DB_PATH = os.path.join(DB_DIR, "podcast.db") |
|
|
| def get_db_connection(): |
| conn = sqlite3.connect(DB_PATH, timeout=20) |
| conn.row_factory = sqlite3.Row |
| return conn |
|
|
| def init_db(): |
| try: |
| with get_db_connection() as conn: |
| conn.execute(''' |
| CREATE TABLE IF NOT EXISTS users ( |
| fingerprint TEXT PRIMARY KEY, |
| ips TEXT NOT NULL, |
| count INTEGER NOT NULL, |
| week_start REAL NOT NULL |
| ) |
| ''') |
| conn.commit() |
| logging.info(f"✅ دیتابیس SQLite با موفقیت در مسیر زیر متصل شد: {DB_PATH}") |
| except Exception as e: |
| logging.error(f"❌ خطا در ساخت دیتابیس SQLite: {e}") |
|
|
| def get_user_record(conn, fingerprint, ip): |
| cursor = conn.execute('SELECT * FROM users WHERE fingerprint = ?', (fingerprint,)) |
| row = cursor.fetchone() |
| if row: |
| return dict(row) |
| |
| ip_search = f'%"{ip}"%' |
| cursor = conn.execute('SELECT * FROM users WHERE ips LIKE ?', (ip_search,)) |
| row = cursor.fetchone() |
| if row: |
| return dict(row) |
| |
| return None |
|
|
| def background_cleanup(): |
| while True: |
| time.sleep(86400) |
| try: |
| six_months_ago = time.time() - (6 * 30 * 24 * 60 * 60) |
| with get_db_connection() as conn: |
| conn.execute('DELETE FROM users WHERE week_start < ?', (six_months_ago,)) |
| conn.commit() |
| except Exception as e: |
| logging.error(f"DB Cleanup error: {e}") |
|
|
| def get_user_ip(): |
| if request.headers.getlist("X-Forwarded-For"): |
| return request.headers.getlist("X-Forwarded-For")[0].split(',')[0].strip() |
| return request.remote_addr |
|
|
| |
| def call_worker(index, chunk_payload): |
| raw_text = chunk_payload.get("text", "") |
| |
| clean_text = re.sub(r'[*_`~#]', '', raw_text) |
| clean_text = re.sub(r'\[.*?\]|\(.*?\)', '', clean_text) |
| clean_text = clean_text.strip() |
| |
| if not clean_text: |
| return index, AudioSegment.silent(duration=500) |
|
|
| use_live = True if len(clean_text) <= 600 else False |
| |
| target_speaker = chunk_payload.get("speaker") |
| is_custom = chunk_payload.get("is_custom", False) |
| actual_speaker_request = "Charon" if is_custom else target_speaker |
|
|
| worker_payload = { |
| "text": clean_text, |
| "speaker": actual_speaker_request, |
| "temperature": chunk_payload.get("temperature", 0.9), |
| "use_live_model": use_live, |
| "retry_limit": 50, |
| "fallback_to_live": True |
| } |
|
|
| max_attempts = 40 |
| |
| for attempt in range(max_attempts): |
| workers = list(WORKER_URLS) |
| random.shuffle(workers) |
| |
| for worker_url in workers: |
| try: |
| logging.info(f"▶️ [قطعه {index+1}] ارسال به {worker_url} (تلاش {attempt+1}/{max_attempts})") |
| response = requests.post(worker_url, json=worker_payload, headers=STANDARD_HEADERS, timeout=120) |
| |
| if response.status_code == 200: |
| logging.info(f"✅ [قطعه {index+1}] با موفقیت ساخته شد.") |
| audio_data = io.BytesIO(response.content) |
| audio_segment = AudioSegment.from_file(audio_data) |
| return index, audio_segment |
| |
| else: |
| logging.warning(f"⚠️ [قطعه {index+1}] ارور {response.status_code} از کارگر دریافت شد. تلاش بعدی...") |
| continue |
| |
| except Exception as e: |
| logging.error(f"❌ [قطعه {index+1}] قطعی ارتباط: {str(e)[:40]}") |
| continue |
| |
| sleep_time = 3 + (attempt * 1.5) + random.uniform(0.5, 2.0) |
| logging.warning(f"🔄 [قطعه {index+1}] کارگرها شلوغ/خطا دادند. استراحت به مدت {sleep_time:.1f} ثانیه...") |
| time.sleep(sleep_time) |
| |
| raise ValueError(f"قطعه {index+1} پس از 40 بار تلاش ساخته نشد.") |
|
|
| |
| def generate_podcast_in_background(task_id, system_prompt, safety_settings): |
| try: |
| AYA_SPACE_URL = "https://coherelabs-aya-expanse.hf.space/gradio_api" |
| MAX_ATTEMPTS = 50 |
| |
| for attempt in range(MAX_ATTEMPTS): |
| try: |
| session_hash = str(uuid.uuid4())[0:11] |
| payload = { |
| "data": [system_prompt, [], None, None], |
| "event_data": None, "fn_index": 2, "session_hash": session_hash, "trigger_id": 37 |
| } |
|
|
| join_response = requests.post(f"{AYA_SPACE_URL}/queue/join", json=payload, headers=STANDARD_HEADERS, timeout=60) |
| join_response.raise_for_status() |
| |
| stream_url = f"{AYA_SPACE_URL}/queue/data?session_hash={session_hash}" |
| raw_text = None |
| |
| with requests.get(stream_url, stream=True, headers=STANDARD_HEADERS, timeout=120) as resp: |
| for line in resp.iter_lines(): |
| if line: |
| decoded_line = line.decode('utf-8') |
| if decoded_line.startswith('data: '): |
| json_str = decoded_line[6:] |
| try: |
| msg_data = json.loads(json_str) |
| msg_type = msg_data.get('msg') |
| if msg_type == 'process_completed': |
| output = msg_data.get('output', {}).get('data', []) |
| if output and len(output) > 0: |
| updated_history = output[0] |
| if updated_history: |
| raw_text = updated_history[-1][1] |
| elif msg_type == 'queue_full': |
| raise Exception("صف سرور شلوغ است") |
| except json.JSONDecodeError: continue |
|
|
| if not raw_text: raise ValueError("بدون پاسخ از مدل هوش مصنوعی") |
|
|
| json_string = None |
| json_pattern = r"`{3}json\s*(\{.*?\})\s*`{3}" |
| match = re.search(json_pattern, raw_text, re.DOTALL) |
| if match: json_string = match.group(1) |
| else: |
| s_idx = raw_text.find('{') |
| e_idx = raw_text.rfind('}') |
| if s_idx != -1 and e_idx != -1: json_string = raw_text[s_idx:e_idx+1] |
| |
| if not json_string: raise ValueError("No JSON found in response") |
| |
| data = json.loads(json_string) |
| if "script" in data: |
| for t in data["script"]: |
| if "dialogue" in t: |
| t["dialogue"] = re.sub(r'\[.*?\]|\(.*?\)', '', t["dialogue"]).strip() |
|
|
| with tasks_lock: |
| tasks[task_id].update({'status': 'completed', 'data': data}) |
| return |
| |
| except Exception as e: |
| logging.warning(f"AI Attempt {attempt} failed: {e}") |
| time.sleep(2) |
| |
| with tasks_lock: tasks[task_id].update({'status': 'failed', 'error': 'Max retries reached'}) |
|
|
| except Exception as e: |
| with tasks_lock: tasks[task_id].update({'status': 'failed', 'error': str(e)}) |
|
|
| |
| def generate_full_podcast_audio_background(task_id, prompt, speakers): |
| try: |
| logging.info(f"🚀 [پروژه {task_id}] عملیات ساخت پادکست آغاز شد.") |
| with tasks_lock: |
| tasks[task_id] = {'status': 'writing_script', 'progress': 'در حال نگارش سناریو...'} |
| |
| valid_speaker_ids = [str(s['id']).strip() for s in speakers] |
| default_speaker_id = valid_speaker_ids[0] if valid_speaker_ids else "Charon" |
| spk_text = "\n".join([f"- {s['id']}: {s['name']}" for s in speakers]) |
| system_prompt = f"""Act as a Professional Podcast Producer. |
| Topic: "{prompt}" |
| Speakers Available: |
| {spk_text} |
| |
| CRITICAL INSTRUCTION: You must create a VERY LONG, in-depth, and highly detailed podcast script. |
| - Do NOT write a short summary. |
| - The conversation must deeply explore the topic. Make it a very long and detailed conversation, as long as necessary to fully cover the topic. |
| - Make the dialogue engaging and informative. You must use two speakers for the podcast, or three if necessary, or whatever number of speakers the user specifies. The podcast should be an engaging exchange, typically between a man and a woman, with both speakers discussing the topic together. |
| |
| Output ONLY valid JSON. |
| Format: {{"selected_speakers": ["id1", "id2"], "script": [{{"speaker_id": "id1", "dialogue": "..."}}]}} |
| Dialogue rules: Do NOT avoid writing stage directions and emotional cues like laughing or sighing. They are allowed, but the mood/action MUST be placed inside parentheses, for example (laugh) or (sigh).""" |
|
|
| AYA_SPACE_URL = "https://coherelabs-aya-expanse.hf.space/gradio_api" |
| MAX_ATTEMPTS = 50 |
| data = None |
| |
| logging.info("📝 در حال ارتباط با هوش مصنوعی برای نوشتن سناریو...") |
| for attempt in range(MAX_ATTEMPTS): |
| try: |
| session_hash = str(uuid.uuid4())[0:11] |
| payload = {"data": [system_prompt, [], None, None], "event_data": None, "fn_index": 2, "session_hash": session_hash, "trigger_id": 37} |
| join_response = requests.post(f"{AYA_SPACE_URL}/queue/join", json=payload, headers=STANDARD_HEADERS, timeout=60) |
| join_response.raise_for_status() |
| |
| stream_url = f"{AYA_SPACE_URL}/queue/data?session_hash={session_hash}" |
| raw_text = None |
| with requests.get(stream_url, stream=True, headers=STANDARD_HEADERS, timeout=120) as resp: |
| for line in resp.iter_lines(): |
| if line: |
| decoded_line = line.decode('utf-8') |
| if decoded_line.startswith('data: '): |
| json_str = decoded_line[6:] |
| try: |
| msg_data = json.loads(json_str) |
| if msg_data.get('msg') == 'process_completed': |
| output = msg_data.get('output', {}).get('data', []) |
| if output and len(output) > 0 and output[0]: |
| raw_text = output[0][-1][1] |
| except: pass |
| |
| if raw_text: |
| json_string = None |
| json_pattern = r"`{3}json\s*(\{.*?\})\s*`{3}" |
| match = re.search(json_pattern, raw_text, re.DOTALL) |
| if match: json_string = match.group(1) |
| else: |
| s_idx = raw_text.find('{') |
| e_idx = raw_text.rfind('}') |
| if s_idx != -1 and e_idx != -1: json_string = raw_text[s_idx:e_idx+1] |
| if json_string: |
| data = json.loads(json_string) |
| break |
| except Exception as e: |
| time.sleep(2) |
| |
| if not data or "script" not in data: |
| raise ValueError("هوش مصنوعی در نوشتن سناریو با خطا مواجه شد.") |
| |
| for t in data["script"]: |
| if "dialogue" in t: t["dialogue"] = re.sub(r'\[.*?\]|\(.*?\)', '', t["dialogue"]).strip() |
|
|
| script_turns = [t for t in data.get("script", []) if str(t.get("dialogue", "")).strip()] |
| total_turns = len(script_turns) |
| |
| logging.info(f"✅ سناریو استخراج شد! شامل {total_turns} نوبت گفتگو.") |
| |
| with tasks_lock: |
| tasks[task_id] = {'status': 'generating_audio', 'progress': f'در حال تولید صداها (0 از {total_turns} تکمیل شده)'} |
| |
| audio_results = [None] * total_turns |
| completed_count = 0 |
| |
| def process_single_chunk(index, turn_data): |
| raw_speaker_id = str(turn_data.get("speaker_id", "")).strip() |
| final_speaker_id = raw_speaker_id |
| |
| if final_speaker_id not in valid_speaker_ids: |
| found = False |
| for v_id in valid_speaker_ids: |
| if final_speaker_id.lower() == v_id.lower(): |
| final_speaker_id = v_id |
| found = True |
| break |
| if not found: |
| logging.warning(f"⚠️ آیدی گوینده نامعتبر '{raw_speaker_id}' تشخیص داده شد. جایگزین شد با '{default_speaker_id}'.") |
| final_speaker_id = default_speaker_id |
|
|
| dialogue = turn_data.get("dialogue") |
| payload = {"text": dialogue, "speaker": final_speaker_id, "temperature": 0.9, "is_custom": False} |
| idx, audio_seg = call_worker(index, payload) |
| if audio_seg is None: |
| raise ValueError(f"خطا در تولید صدای نوبت {index+1}") |
| return idx, audio_seg |
|
|
| batch_size = 7 |
| batches = [script_turns[i:i + batch_size] for i in range(0, total_turns, batch_size)] |
| |
| for batch_index, batch in enumerate(batches): |
| logging.info(f"🔄 شروع پردازش دستهی {batch_index+1} از {len(batches)} (شامل {len(batch)} دیالوگ)...") |
| |
| with concurrent.futures.ThreadPoolExecutor(max_workers=len(batch)) as executor: |
| start_idx = batch_index * batch_size |
| future_to_index = {executor.submit(process_single_chunk, start_idx + i, turn): start_idx + i for i, turn in enumerate(batch)} |
| |
| for future in concurrent.futures.as_completed(future_to_index): |
| try: |
| idx, audio_seg = future.result() |
| audio_results[idx] = audio_seg |
| completed_count += 1 |
| with tasks_lock: |
| tasks[task_id]['progress'] = f'در حال تولید صداها ({completed_count} از {total_turns} تکمیل شده)' |
| except Exception as exc: |
| logging.error(f"❌ خطای بحرانی: {str(exc)}") |
| raise Exception(str(exc)) |
| |
| logging.info(f"✔️ دستهی {batch_index+1} با موفقیت تمام شد.") |
|
|
| logging.info("✂️ تمام قطعات ساخته شد. در حال ادغام (میکس) فایلها...") |
| combined_audio = AudioSegment.empty() |
| for seg in audio_results: |
| if seg: |
| combined_audio += seg |
|
|
| with tasks_lock: |
| tasks[task_id] = {'status': 'mixing', 'progress': 'در حال میکس نهایی و ذخیره پادکست...'} |
| |
| filename = f"full_podcast_{task_id}.mp3" |
| filepath = os.path.join(CACHE_DIRECTORY, filename) |
| |
| output_buffer = io.BytesIO() |
| combined_audio.export(output_buffer, format="mp3", bitrate="192k") |
| output_buffer.seek(0) |
| |
| with open(filepath, 'wb') as f: |
| f.write(output_buffer.read()) |
| |
| logging.info(f"🎉 پادکست نهایی آماده شد! نام فایل: {filename}") |
| with tasks_lock: |
| tasks[task_id] = {'status': 'completed', 'filename': filename, 'progress': 'پادکست شما آماده است!'} |
|
|
| except Exception as e: |
| logging.error(f"❌ پردازش کل پادکست با شکست مواجه شد: {str(e)}") |
| with tasks_lock: |
| tasks[task_id] = {'status': 'failed', 'error': str(e)} |
|
|
| |
| def process_voice_conversion(tts_audio_io, ref_audio_base64): |
| try: |
| tts_audio_io.seek(0) |
| if "," in ref_audio_base64: ref_audio_base64 = ref_audio_base64.split(",")[1] |
| ref_bytes = base64.b64decode(ref_audio_base64) |
| |
| files = { |
| 'source_audio': ('source.wav', tts_audio_io, 'audio/wav'), |
| 'ref_audio': ('ref.wav', io.BytesIO(ref_bytes), 'audio/wav') |
| } |
| res = requests.post(f"{VC_SPACE_URL}/upload", files=files, headers=STANDARD_HEADERS, timeout=120) |
| if res.status_code != 200: raise Exception(f"VC Upload Failed: {res.text}") |
| |
| job_data = res.json() |
| for _ in range(120): |
| time.sleep(4) |
| chk = requests.post(f"{VC_SPACE_URL}/check_status", json=job_data, headers=STANDARD_HEADERS, timeout=30) |
| if chk.status_code == 200: |
| stat = chk.json() |
| if stat.get("status") == "completed": |
| filename = stat.get("filename") |
| dl = requests.get(f"{VC_SPACE_URL}/download/{filename}", headers=STANDARD_HEADERS) |
| if dl.status_code == 200: return io.BytesIO(dl.content) |
| else: raise Exception("VC Download Failed") |
| elif stat.get("status") == "failed": |
| raise Exception(f"VC Remote Failed: {stat.get('detail', 'Unknown error')}") |
| |
| raise Exception("VC Timeout (Processing took too long)") |
| except Exception as e: |
| logging.error(f"VC Error: {e}") |
| return None |
|
|
| |
| def build_fast_scenario(prompt): |
| spk_text = "- Charon: شهاب (مرد)\n- Zephyr: آوا (زن)" |
| system_prompt = f"""Act as a Professional Podcast Producer.\nTopic: "{prompt}"\nSpeakers Available:\n{spk_text}\nCRITICAL INSTRUCTION: You must create a VERY LONG, in-depth, and highly detailed podcast script.\nOutput ONLY valid JSON.\nFormat: {{"selected_speakers": ["Charon", "Zephyr"], "script": [{{"speaker_id": "Charon", "dialogue": "..."}}]}}""" |
| |
| AYA_SPACE_URL = "https://coherelabs-aya-expanse.hf.space/gradio_api" |
| for attempt in range(5): |
| try: |
| session_hash = str(uuid.uuid4())[0:11] |
| payload = {"data": [system_prompt, [], None, None], "event_data": None, "fn_index": 2, "session_hash": session_hash, "trigger_id": 37} |
| join_response = requests.post(f"{AYA_SPACE_URL}/queue/join", json=payload, headers=STANDARD_HEADERS, timeout=60) |
| join_response.raise_for_status() |
| |
| stream_url = f"{AYA_SPACE_URL}/queue/data?session_hash={session_hash}" |
| raw_text = None |
| with requests.get(stream_url, stream=True, headers=STANDARD_HEADERS, timeout=120) as resp: |
| for line in resp.iter_lines(): |
| if line: |
| decoded_line = line.decode('utf-8') |
| if decoded_line.startswith('data: '): |
| try: |
| msg_data = json.loads(decoded_line[6:]) |
| if msg_data.get('msg') == 'process_completed': |
| output = msg_data.get('output', {}).get('data', []) |
| if output and len(output) > 0 and output[0]: |
| raw_text = output[0][-1][1] |
| except: pass |
| |
| if raw_text: |
| json_string = None |
| match = re.search(r"`{3}json\s*(\{.*?\})\s*`{3}", raw_text, re.DOTALL) |
| if match: json_string = match.group(1) |
| else: |
| s_idx = raw_text.find('{') |
| e_idx = raw_text.rfind('}') |
| if s_idx != -1 and e_idx != -1: json_string = raw_text[s_idx:e_idx+1] |
| |
| if json_string: |
| data = json.loads(json_string) |
| return data |
| except Exception as e: |
| time.sleep(2) |
| raise ValueError("تولید سناریو با مشکل مواجه شد") |
|
|
| |
| @app.route('/') |
| def index(): |
| return render_template('index.html') |
|
|
| @app.route('/api/fast/tts', methods=['GET', 'POST']) |
| def fast_tts(): |
| try: |
| data = request.args if request.method == 'GET' else request.get_json(silent=True) or request.form |
| text = data.get("text", "").strip() |
| speaker = data.get("speaker", "Charon") |
| |
| if not text: |
| return jsonify({"error": "پارامتر text الزامی است"}), 400 |
| |
| payload = {"text": text, "speaker": speaker, "temperature": 0.9, "is_custom": False} |
| idx, audio_seg = call_worker(0, payload) |
| |
| if audio_seg is None: |
| return jsonify({"error": "خطا در تولید صدا"}), 500 |
| |
| mp3_buffer = io.BytesIO() |
| audio_seg.export(mp3_buffer, format="mp3", bitrate="192k") |
| mp3_buffer.seek(0) |
| |
| return send_file(mp3_buffer, mimetype="audio/mpeg", as_attachment=True, download_name=f"tts_{uuid.uuid4()}.mp3") |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route('/api/fast/scenario', methods=['GET', 'POST']) |
| def fast_scenario(): |
| try: |
| data = request.args if request.method == 'GET' else request.get_json(silent=True) or request.form |
| prompt = data.get("prompt", "").strip() |
| |
| if not prompt: |
| return jsonify({"error": "پارامتر prompt الزامی است"}), 400 |
| |
| data = build_fast_scenario(prompt) |
| return jsonify(data) |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route('/api/fast/podcast', methods=['GET', 'POST']) |
| def fast_podcast(): |
| try: |
| data = request.args if request.method == 'GET' else request.get_json(silent=True) or request.form |
| prompt = data.get("prompt", "").strip() |
| |
| if not prompt: |
| return jsonify({"error": "پارامتر prompt الزامی است"}), 400 |
| |
| script_data = build_fast_scenario(prompt) |
| script_turns = script_data.get("script", []) |
| |
| valid_speaker_ids = ["Charon", "Zephyr"] |
| audio_results = [None] * len(script_turns) |
| |
| def process_chunk(index, turn_data): |
| spk = turn_data.get("speaker_id", "Charon") |
| if spk not in valid_speaker_ids: spk = "Charon" |
| payload = {"text": turn_data.get("dialogue", ""), "speaker": spk, "temperature": 0.9, "is_custom": False} |
| idx, audio_seg = call_worker(index, payload) |
| if audio_seg is None: raise ValueError("خطا") |
| return idx, audio_seg |
|
|
| with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: |
| future_to_idx = {executor.submit(process_chunk, i, t): i for i, t in enumerate(script_turns)} |
| for future in concurrent.futures.as_completed(future_to_idx): |
| idx, audio_seg = future.result() |
| audio_results[idx] = audio_seg |
| |
| combined_audio = AudioSegment.empty() |
| for seg in audio_results: |
| if seg: combined_audio += seg |
| |
| mp3_buffer = io.BytesIO() |
| combined_audio.export(mp3_buffer, format="mp3", bitrate="192k") |
| mp3_buffer.seek(0) |
| |
| return send_file(mp3_buffer, mimetype="audio/mpeg", as_attachment=True, download_name=f"podcast_{uuid.uuid4()}.mp3") |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route('/api/check-credit', methods=['POST']) |
| def check_credit(): |
| data = request.get_json() |
| fingerprint = data.get('fingerprint') |
| if not fingerprint: return jsonify({"status": "error"}), 400 |
| |
| ip = get_user_ip() |
| now = time.time() |
| week_ago = now - (7*24*60*60) |
| |
| try: |
| with get_db_connection() as conn: |
| user = get_user_record(conn, fingerprint, ip) |
| if user: |
| if user['week_start'] < week_ago: |
| conn.execute('UPDATE users SET count = 0, week_start = ? WHERE fingerprint = ?', (now, user['fingerprint'])) |
| conn.commit() |
| user['count'] = 0 |
| user['week_start'] = now |
| |
| remaining = USAGE_LIMIT - user['count'] |
| limit_reached = remaining <= 0 |
| reset_ts = user['week_start'] + (7*24*60*60) |
| |
| return jsonify({"credits_remaining": max(0, remaining), "limit_reached": limit_reached, "reset_timestamp": reset_ts}) |
| else: |
| return jsonify({"credits_remaining": USAGE_LIMIT, "limit_reached": False, "reset_timestamp": 0}) |
| except Exception as e: |
| logging.error(f"Error checking credit: {e}") |
| return jsonify({"status": "error"}), 500 |
|
|
| @app.route('/api/use-credit', methods=['POST']) |
| def use_credit(): |
| data = request.get_json() |
| fingerprint = data.get('fingerprint') |
| if not fingerprint: return jsonify({"status": "error"}), 400 |
| |
| ip = get_user_ip() |
| now = time.time() |
| week_ago = now - (7*24*60*60) |
| |
| try: |
| with get_db_connection() as conn: |
| user = get_user_record(conn, fingerprint, ip) |
| |
| if user: |
| if user['week_start'] < week_ago: |
| user['count'] = 0 |
| user['week_start'] = now |
| |
| if user['count'] >= USAGE_LIMIT: |
| return jsonify({"status": "limit"}), 429 |
| |
| user['count'] += 1 |
| ips = json.loads(user['ips']) |
| if ip not in ips: |
| ips.append(ip) |
| |
| conn.execute('UPDATE users SET count = ?, week_start = ?, ips = ? WHERE fingerprint = ?', |
| (user['count'], user['week_start'], json.dumps(ips), user['fingerprint'])) |
| conn.commit() |
| return jsonify({"status": "success", "credits_remaining": USAGE_LIMIT - user['count']}) |
| else: |
| conn.execute('INSERT INTO users (fingerprint, ips, count, week_start) VALUES (?, ?, ?, ?)', |
| (fingerprint, json.dumps([ip]), 1, now)) |
| conn.commit() |
| return jsonify({"status": "success", "credits_remaining": USAGE_LIMIT - 1}) |
| except Exception as e: |
| logging.error(f"Error using credit: {e}") |
| return jsonify({"status": "error"}), 500 |
|
|
| @app.route('/api/create-full-podcast', methods=['POST']) |
| def create_full_podcast(): |
| try: |
| data = request.get_json() |
| prompt = data.get('prompt') |
| speakers = data.get('available_speakers') |
| if not prompt or not speakers: return jsonify({"error": "Bad request"}), 400 |
| |
| task_id = str(uuid.uuid4()) |
| with tasks_lock: tasks[task_id] = {'status': 'pending'} |
| |
| safety = [{"category": c, "threshold": "BLOCK_NONE"} for c in ["HARM_CATEGORY_HARASSMENT", "HARM_CATEGORY_HATE_SPEECH", "HARM_CATEGORY_SEXUALLY_EXPLICIT", "HARM_CATEGORY_DANGEROUS_CONTENT"]] |
| spk_text = "\n".join([f"- {s['id']}: {s['name']}" for s in speakers]) |
| |
| sys_prompt = f"""Act as a Professional Podcast Producer.\nTopic: "{prompt}"\nSpeakers Available:\n{spk_text}\nCRITICAL INSTRUCTIONS:\n1. Create a VERY LONG, in-depth podcast script.\n2. Keep EVERY dialogue line SHORT. Break long speeches into multiple consecutive turns for the same speaker.\n3. NO stage directions, NO emojis, NO brackets. Plain text ONLY.\n\nOutput ONLY valid JSON.\nFormat: {{"selected_speakers": ["id1", "id2"], "script": [{{"speaker_id": "id1", "dialogue": "..."}}]}}""" |
| |
| threading.Thread(target=generate_podcast_in_background, args=(task_id, sys_prompt, safety)).start() |
| return jsonify({"task_id": task_id}), 202 |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route('/api/podcast-status/<task_id>', methods=['GET']) |
| def podcast_status(task_id): |
| with tasks_lock: |
| return jsonify(tasks.get(task_id, {'status': 'not_found'})), 200 |
|
|
| @app.route('/api/auto-podcast', methods=['POST']) |
| def auto_podcast(): |
| try: |
| data = request.get_json() |
| prompt = data.get('prompt') |
| speakers = data.get('available_speakers') |
| if not prompt or not speakers: return jsonify({"error": "Bad request"}), 400 |
| |
| task_id = str(uuid.uuid4()) |
| with tasks_lock: tasks[task_id] = {'status': 'pending', 'progress': 'در حال ورود به صف پردازش...'} |
| |
| threading.Thread(target=generate_full_podcast_audio_background, args=(task_id, prompt, speakers)).start() |
| return jsonify({"task_id": task_id}), 202 |
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| @app.route('/api/auto-podcast-status/<task_id>', methods=['GET']) |
| def auto_podcast_status(task_id): |
| with tasks_lock: |
| return jsonify(tasks.get(task_id, {'status': 'not_found'})), 200 |
|
|
| @app.route('/api/download-podcast/<filename>', methods=['GET']) |
| def download_podcast(filename): |
| filepath = os.path.join(CACHE_DIRECTORY, filename) |
| if os.path.exists(filepath): |
| return send_file(filepath, mimetype="audio/mpeg", as_attachment=True) |
| return jsonify({"error": "File not found"}), 404 |
|
|
| @app.route('/api/generate', methods=['POST']) |
| def generate_audio_route(): |
| try: |
| data = request.get_json() |
| if not data: return jsonify({"error": "No data"}), 400 |
| |
| text = data.get("text", "") |
| speaker = data.get("speaker") |
| temperature = data.get("temperature", 0.9) |
| ref_base64 = data.get("ref_audio_base64") |
| |
| if not text: return jsonify({"error": "Text empty"}), 400 |
| |
| is_custom = bool(speaker.startswith("custom_") and ref_base64) |
| payload = {"text": text, "speaker": speaker, "temperature": temperature, "is_custom": is_custom} |
| |
| idx, audio_seg = call_worker(0, payload) |
| if audio_seg is None: return jsonify({"error": "Worker generation failed"}), 503 |
| |
| if is_custom: |
| logging.info("Starting Custom VC...") |
| |
| |
| wav_buffer = io.BytesIO() |
| audio_seg.export(wav_buffer, format="wav") |
| wav_buffer.seek(0) |
| |
| vc_out = process_voice_conversion(wav_buffer, ref_base64) |
| if vc_out: |
| |
| vc_audio = AudioSegment.from_file(vc_out) |
| mp3_buffer = io.BytesIO() |
| vc_audio.export(mp3_buffer, format="mp3", bitrate="192k") |
| mp3_buffer.seek(0) |
| |
| return send_file(mp3_buffer, mimetype="audio/mpeg", as_attachment=True, download_name=f"vc_{uuid.uuid4()}.mp3") |
| else: |
| return jsonify({"error": "Voice Conversion failed"}), 500 |
| |
| |
| mp3_buffer = io.BytesIO() |
| audio_seg.export(mp3_buffer, format="mp3", bitrate="192k") |
| mp3_buffer.seek(0) |
| |
| return send_file(mp3_buffer, mimetype="audio/mpeg", as_attachment=True, download_name=f"gen_{uuid.uuid4()}.mp3") |
|
|
| except Exception as e: |
| logging.error(f"Generate route error: {e}") |
| return jsonify({"error": str(e)}), 500 |
|
|
| |
| init_db() |
| threading.Thread(target=background_cleanup, daemon=True).start() |
|
|
| if __name__ == '__main__': |
| port = int(os.environ.get('PORT', 7860)) |
| app.run(host='0.0.0.0', port=port) |
|
|