Spaces:
Runtime error
Runtime error
Download app.py from gopallikehack/Tgjson123: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/gopallikehack/Tgjson123/resolve/bf51ec00a0af49bcfeb21637e033bcf9a95b7baa/app.py
- Command line
-
hf download hf://spaces/gopallikehack/Tgjson123@bf51ec00a0af49bcfeb21637e033bcf9a95b7baa/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gopallikehack/Tgjson123/resolve/bf51ec00a0af49bcfeb21637e033bcf9a95b7baa/app.py
4.18 kB
| import os | |
| import requests | |
| import json | |
| import base64 | |
| import time | |
| from flask import Flask, request, jsonify, send_file | |
| app = Flask(__name__) | |
| # ===== SECRETS ===== | |
| AGNES_API_KEY = os.environ.get("AGNES_API_KEY") | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| if not AGNES_API_KEY: | |
| print("⚠️ AGNES_API_KEY not set! Get from video.lichuanyang.top") | |
| # ===== AGNES API (Multiple endpoints to try) ===== | |
| AGNES_ENDPOINTS = [ | |
| "https://video.lichuanyang.top/api/generate", | |
| "https://video.lichuanyang.top/v1/generate", | |
| "https://video.lichuanyang.top/generate" | |
| ] | |
| def generate_video_agnes(prompt, duration=45): | |
| """Generate video using Agnes AI API""" | |
| headers = { | |
| "Authorization": f"Bearer {AGNES_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "prompt": prompt, | |
| "duration": min(duration, 60) | |
| } | |
| for endpoint in AGNES_ENDPOINTS: | |
| try: | |
| print(f"🔄 Trying Agnes endpoint: {endpoint}") | |
| response = requests.post(endpoint, json=payload, headers=headers, timeout=120) | |
| if response.status_code == 200: | |
| data = response.json() | |
| # Check for video URL or base64 | |
| video_url = data.get("video_url") or data.get("url") or data.get("output") | |
| if video_url: | |
| return video_url | |
| video_b64 = data.get("video_base64") or data.get("data") | |
| if video_b64: | |
| return f"data:video/mp4;base64,{video_b64}" | |
| # If response is direct video bytes | |
| if response.headers.get("content-type", "").startswith("video/"): | |
| video_b64 = base64.b64encode(response.content).decode('utf-8') | |
| return f"data:video/mp4;base64,{video_b64}" | |
| print(f"⚠️ No video data in response: {data}") | |
| else: | |
| print(f"❌ Agnes endpoint {endpoint} failed: {response.status_code} - {response.text[:100]}") | |
| except Exception as e: | |
| print(f"❌ Agnes endpoint error: {str(e)}") | |
| raise Exception("All Agnes endpoints failed") | |
| # ===== HUGGING FACE TTS (optional audio) ===== | |
| def generate_audio_hf(text, lang, voice): | |
| try: | |
| if not HF_TOKEN: | |
| return None | |
| model = "facebook/mms-tts-hin" if lang == 'hi' else "facebook/mms-tts-eng" | |
| url = f"https://api-inference.huggingface.co/models/{model}" | |
| headers = { | |
| "Authorization": f"Bearer {HF_TOKEN}", | |
| "Content-Type": "application/json" | |
| } | |
| response = requests.post( | |
| url, | |
| headers=headers, | |
| json={"inputs": text[:500], "parameters": {"voice": voice, "speed": 1.0}}, | |
| timeout=45 | |
| ) | |
| if response.status_code == 200: | |
| audio_b64 = base64.b64encode(response.content).decode('utf-8') | |
| return f"data:audio/mp3;base64,{audio_b64}" | |
| return None | |
| except: | |
| return None | |
| # ===== ROUTES ===== | |
| def index(): | |
| return send_file('index.html') | |
| def generate_video(): | |
| try: | |
| data = request.json | |
| prompt = data.get('prompt') | |
| language = data.get('language', 'en') | |
| duration = int(data.get('duration', 45)) | |
| voice_en = data.get('voice_en', 'en-US-JennyNeural') | |
| voice_hi = data.get('voice_hi', 'hi-IN-SwaraNeural') | |
| if not prompt: | |
| return jsonify({'error': 'Prompt is required'}), 400 | |
| if not AGNES_API_KEY: | |
| return jsonify({'error': 'AGNES_API_KEY not configured'}), 500 | |
| # Generate video | |
| video_url = generate_video_agnes(prompt, duration) | |
| # Generate audio (optional) | |
| audio_url = generate_audio_hf(prompt, language, voice_en if language == 'en' else voice_hi) | |
| return jsonify({ | |
| 'success': True, | |
| 'video_url': video_url, | |
| 'audio_url': audio_url or '' | |
| }) | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 500 | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=False) |