Update Gradio app with multiple files
Browse files- app.py +58 -41
- requirements.txt +10 -1
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import requests
|
|
| 7 |
from typing import Optional, Tuple, Dict, Any
|
| 8 |
import tempfile
|
| 9 |
import base64
|
|
|
|
| 10 |
|
| 11 |
# Initialize OpenAI client with Poe API configuration
|
| 12 |
client = openai.OpenAI(
|
|
@@ -23,6 +24,27 @@ def format_sora_prompt(
|
|
| 23 |
formatted_prompt = f"{prompt}\n\n--duration {duration} --size {size}"
|
| 24 |
return formatted_prompt
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def generate_video(
|
| 27 |
prompt: str,
|
| 28 |
duration: int = 8,
|
|
@@ -43,15 +65,12 @@ def generate_video(
|
|
| 43 |
else:
|
| 44 |
temp_client = client
|
| 45 |
if not os.getenv("POE_API_KEY") and not api_key:
|
| 46 |
-
return None, "❌ Please
|
| 47 |
|
| 48 |
# Format prompt with parameters
|
| 49 |
formatted_prompt = format_sora_prompt(prompt, duration, size)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
status_message = "🎬 Initiating video generation with Sora-2..."
|
| 53 |
-
|
| 54 |
-
# Call Sora-2 through Poe API - simplified API call
|
| 55 |
chat = temp_client.chat.completions.create(
|
| 56 |
model="Sora-2",
|
| 57 |
messages=[{"role": "user", "content": formatted_prompt}],
|
|
@@ -60,50 +79,48 @@ def generate_video(
|
|
| 60 |
# Extract the response content
|
| 61 |
content = chat.choices[0].message.content
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
# In production, you'd parse the actual video data from the response
|
| 66 |
-
|
| 67 |
-
# Create a placeholder video file (in production, save actual video data)
|
| 68 |
-
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
|
| 69 |
-
video_path = tmp_file.name
|
| 70 |
-
|
| 71 |
-
# If the response contains a URL, download it
|
| 72 |
-
if content.startswith("http"):
|
| 73 |
-
video_response = requests.get(content)
|
| 74 |
-
tmp_file.write(video_response.content)
|
| 75 |
-
status_message = f"✅ Video generated successfully! Duration: {duration}s, Size: {size}"
|
| 76 |
-
else:
|
| 77 |
-
# If it's base64 or other format, handle accordingly
|
| 78 |
-
status_message = "✅ Video generation completed!"
|
| 79 |
-
# For demo purposes, write placeholder
|
| 80 |
-
tmp_file.write(b"Video data would be here")
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
error_msg = f"❌ Error generating video: {str(e)}"
|
| 86 |
return None, error_msg
|
| 87 |
|
| 88 |
-
def validate_api_key(api_key: str) -> bool:
|
| 89 |
-
"""Validate if the provided API key works."""
|
| 90 |
-
try:
|
| 91 |
-
test_client = openai.OpenAI(
|
| 92 |
-
api_key=api_key,
|
| 93 |
-
base_url="https://api.poe.com/v1",
|
| 94 |
-
)
|
| 95 |
-
# Try a simple test request
|
| 96 |
-
test_client.chat.completions.create(
|
| 97 |
-
model="Sora-2",
|
| 98 |
-
messages=[{"role": "user", "content": "test"}],
|
| 99 |
-
)
|
| 100 |
-
return True
|
| 101 |
-
except:
|
| 102 |
-
return False
|
| 103 |
-
|
| 104 |
def simple_generate(prompt: str) -> Optional[str]:
|
| 105 |
"""Simplified wrapper for generate_video that only takes prompt and returns video."""
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return video_path
|
| 108 |
|
| 109 |
# Build the Gradio interface
|
|
|
|
| 7 |
from typing import Optional, Tuple, Dict, Any
|
| 8 |
import tempfile
|
| 9 |
import base64
|
| 10 |
+
import re
|
| 11 |
|
| 12 |
# Initialize OpenAI client with Poe API configuration
|
| 13 |
client = openai.OpenAI(
|
|
|
|
| 24 |
formatted_prompt = f"{prompt}\n\n--duration {duration} --size {size}"
|
| 25 |
return formatted_prompt
|
| 26 |
|
| 27 |
+
def extract_video_url(content: str) -> Optional[str]:
|
| 28 |
+
"""Extract video URL from the response content."""
|
| 29 |
+
# Try to find direct video URL
|
| 30 |
+
url_pattern = r'https?://[^\s<>"{}|\\^`\[\]]+\.(?:mp4|mov|avi|webm)'
|
| 31 |
+
urls = re.findall(url_pattern, content)
|
| 32 |
+
if urls:
|
| 33 |
+
return urls[0]
|
| 34 |
+
|
| 35 |
+
# Try to find any URL that might be a video
|
| 36 |
+
general_url_pattern = r'https?://[^\s<>"{}|\\^`\[\]]+'
|
| 37 |
+
urls = re.findall(general_url_pattern, content)
|
| 38 |
+
for url in urls:
|
| 39 |
+
if any(vid_keyword in url.lower() for vid_keyword in ['video', 'mp4', 'mov', 'media', 'cdn']):
|
| 40 |
+
return url
|
| 41 |
+
|
| 42 |
+
# Return first URL if found
|
| 43 |
+
if urls:
|
| 44 |
+
return urls[0]
|
| 45 |
+
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
def generate_video(
|
| 49 |
prompt: str,
|
| 50 |
duration: int = 8,
|
|
|
|
| 65 |
else:
|
| 66 |
temp_client = client
|
| 67 |
if not os.getenv("POE_API_KEY") and not api_key:
|
| 68 |
+
return None, "❌ Please set POE_API_KEY environment variable."
|
| 69 |
|
| 70 |
# Format prompt with parameters
|
| 71 |
formatted_prompt = format_sora_prompt(prompt, duration, size)
|
| 72 |
|
| 73 |
+
# Call Sora-2 through Poe API
|
|
|
|
|
|
|
|
|
|
| 74 |
chat = temp_client.chat.completions.create(
|
| 75 |
model="Sora-2",
|
| 76 |
messages=[{"role": "user", "content": formatted_prompt}],
|
|
|
|
| 79 |
# Extract the response content
|
| 80 |
content = chat.choices[0].message.content
|
| 81 |
|
| 82 |
+
# Try to extract video URL from response
|
| 83 |
+
video_url = extract_video_url(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
if video_url:
|
| 86 |
+
# Download the video
|
| 87 |
+
try:
|
| 88 |
+
headers = {
|
| 89 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 90 |
+
}
|
| 91 |
+
video_response = requests.get(video_url, headers=headers, timeout=60)
|
| 92 |
+
video_response.raise_for_status()
|
| 93 |
+
|
| 94 |
+
# Save to temporary file
|
| 95 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
|
| 96 |
+
tmp_file.write(video_response.content)
|
| 97 |
+
video_path = tmp_file.name
|
| 98 |
+
|
| 99 |
+
status_message = f"✅ Video generated successfully!"
|
| 100 |
+
return video_path, status_message
|
| 101 |
+
|
| 102 |
+
except requests.exceptions.RequestException as e:
|
| 103 |
+
return None, f"❌ Failed to download video: {str(e)}"
|
| 104 |
+
else:
|
| 105 |
+
# If no URL found, the response might contain the video data directly
|
| 106 |
+
# or might need different parsing
|
| 107 |
+
return None, f"❌ Could not extract video from response. Response: {content[:200]}..."
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
error_msg = f"❌ Error generating video: {str(e)}"
|
| 111 |
return None, error_msg
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def simple_generate(prompt: str) -> Optional[str]:
|
| 114 |
"""Simplified wrapper for generate_video that only takes prompt and returns video."""
|
| 115 |
+
if not prompt or prompt.strip() == "":
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
video_path, status = generate_video(prompt, duration=8, size="1280x720", api_key=None)
|
| 119 |
+
|
| 120 |
+
# If there's an error, you might want to handle it
|
| 121 |
+
if video_path is None:
|
| 122 |
+
print(f"Generation failed: {status}")
|
| 123 |
+
|
| 124 |
return video_path
|
| 125 |
|
| 126 |
# Build the Gradio interface
|
requirements.txt
CHANGED
|
@@ -2,4 +2,13 @@ gradio>=5.0.0
|
|
| 2 |
openai>=1.0.0
|
| 3 |
requests>=2.31.0
|
| 4 |
numpy>=1.24.0
|
| 5 |
-
Pillow>=10.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
openai>=1.0.0
|
| 3 |
requests>=2.31.0
|
| 4 |
numpy>=1.24.0
|
| 5 |
+
Pillow>=10.0.0
|
| 6 |
+
|
| 7 |
+
The main changes I made:
|
| 8 |
+
1. Added `extract_video_url()` function to properly parse video URLs from the Poe API response
|
| 9 |
+
2. Improved error handling to show what's in the response if video extraction fails
|
| 10 |
+
3. Added proper headers for downloading videos
|
| 11 |
+
4. Added validation for empty prompts
|
| 12 |
+
5. Better error messages to help debug issues
|
| 13 |
+
|
| 14 |
+
This should properly handle the Poe API response and extract/download the generated video.
|