|
|
|
|
|
|
|
|
import os |
|
|
import sys |
|
|
import traceback |
|
|
import tempfile |
|
|
import warnings |
|
|
warnings.filterwarnings('ignore') |
|
|
|
|
|
print("π Starting ULTRA-SAFE mode...") |
|
|
print(f"Python: {sys.version}") |
|
|
|
|
|
|
|
|
IMPORTS = { |
|
|
'torch': False, |
|
|
'numpy': False, |
|
|
'PIL': False, |
|
|
'gradio': False, |
|
|
'soundfile': False, |
|
|
'transformers': False |
|
|
} |
|
|
|
|
|
try: |
|
|
import torch |
|
|
IMPORTS['torch'] = True |
|
|
print("β
PyTorch imported") |
|
|
except Exception as e: |
|
|
print(f"β PyTorch failed: {e}") |
|
|
|
|
|
try: |
|
|
import numpy as np |
|
|
IMPORTS['numpy'] = True |
|
|
print("β
NumPy imported") |
|
|
except Exception as e: |
|
|
print(f"β NumPy failed: {e}") |
|
|
|
|
|
try: |
|
|
from PIL import Image |
|
|
IMPORTS['PIL'] = True |
|
|
print("β
PIL imported") |
|
|
except Exception as e: |
|
|
print(f"β PIL failed: {e}") |
|
|
|
|
|
try: |
|
|
import gradio as gr |
|
|
IMPORTS['gradio'] = True |
|
|
print("β
Gradio imported") |
|
|
except Exception as e: |
|
|
print(f"β Gradio failed: {e}") |
|
|
|
|
|
try: |
|
|
import soundfile as sf |
|
|
IMPORTS['soundfile'] = True |
|
|
print("β
SoundFile imported") |
|
|
except Exception as e: |
|
|
print(f"β SoundFile failed: {e}") |
|
|
|
|
|
|
|
|
if IMPORTS['torch']: |
|
|
try: |
|
|
from transformers import BlipProcessor, BlipForConditionalGeneration |
|
|
from transformers import MusicgenForConditionalGeneration, AutoProcessor |
|
|
IMPORTS['transformers'] = True |
|
|
print("β
Transformers imported") |
|
|
except Exception as e: |
|
|
print(f"β Transformers failed: {e}") |
|
|
|
|
|
class SafeGenerator: |
|
|
def __init__(self): |
|
|
print("π§ Initializing Safe Generator...") |
|
|
self.working = True |
|
|
|
|
|
def safe_string_test(self, input_val): |
|
|
"""Test string operations safely""" |
|
|
print(f"π§ͺ Testing string operations with: {input_val} (type: {type(input_val)})") |
|
|
|
|
|
try: |
|
|
|
|
|
if input_val is None: |
|
|
str_val = "none" |
|
|
elif isinstance(input_val, str): |
|
|
str_val = input_val |
|
|
elif isinstance(input_val, (int, float, bool)): |
|
|
str_val = str(input_val) |
|
|
else: |
|
|
str_val = repr(input_val) |
|
|
|
|
|
print(f"β
String conversion: '{str_val}'") |
|
|
|
|
|
|
|
|
lower_val = str_val.lower() |
|
|
print(f"β
Lower operation: '{lower_val}'") |
|
|
|
|
|
|
|
|
test_keywords = ['test', 'image', 'bright'] |
|
|
results = [] |
|
|
|
|
|
for keyword in test_keywords: |
|
|
try: |
|
|
result = keyword in lower_val |
|
|
results.append(f"{keyword}: {result}") |
|
|
print(f"β
'{keyword}' in '{lower_val}': {result}") |
|
|
except Exception as e: |
|
|
error_msg = f"β Error with '{keyword}': {e}" |
|
|
results.append(error_msg) |
|
|
print(error_msg) |
|
|
|
|
|
return f"String test passed. Results: {', '.join(results)}" |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"β String test failed: {e}\n{traceback.format_exc()}" |
|
|
print(error_msg) |
|
|
return error_msg |
|
|
|
|
|
def create_simple_description(self, image): |
|
|
"""Create description without any AI models""" |
|
|
try: |
|
|
if not IMPORTS['numpy'] or not IMPORTS['PIL']: |
|
|
return "simple image", "simple image, ambient music" |
|
|
|
|
|
print("π Creating simple description...") |
|
|
|
|
|
|
|
|
img_array = np.array(image) |
|
|
h, w, c = img_array.shape |
|
|
print(f"π Image dimensions: {w}x{h}x{c}") |
|
|
|
|
|
|
|
|
try: |
|
|
mean_color = np.mean(img_array, axis=(0, 1)) |
|
|
brightness = float(np.mean(mean_color)) |
|
|
print(f"π‘ Brightness: {brightness}") |
|
|
|
|
|
|
|
|
if brightness > 200: |
|
|
brightness_desc = "bright" |
|
|
elif brightness < 100: |
|
|
brightness_desc = "dark" |
|
|
else: |
|
|
brightness_desc = "medium" |
|
|
|
|
|
print(f"π·οΈ Brightness category: {brightness_desc}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β οΈ Color analysis failed: {e}") |
|
|
brightness_desc = "colorful" |
|
|
|
|
|
|
|
|
base_desc = f"{brightness_desc} image" |
|
|
enhanced_desc = f"{base_desc}, peaceful music" |
|
|
|
|
|
print(f"π Description: '{base_desc}' -> '{enhanced_desc}'") |
|
|
|
|
|
|
|
|
test_result = self.safe_string_test(base_desc) |
|
|
print(f"π§ͺ String test result: {test_result}") |
|
|
|
|
|
return base_desc, enhanced_desc |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"β Description creation failed: {e}\n{traceback.format_exc()}" |
|
|
print(error_msg) |
|
|
return "error image", "error image, quiet music" |
|
|
|
|
|
def create_simple_audio(self, duration=10): |
|
|
"""Create simple audio without AI""" |
|
|
try: |
|
|
if not IMPORTS['numpy']: |
|
|
print("β NumPy not available for audio") |
|
|
return None, None |
|
|
|
|
|
print(f"π΅ Creating {duration}s simple audio...") |
|
|
|
|
|
|
|
|
sample_rate = 22050 |
|
|
t = np.linspace(0, duration, int(duration * sample_rate)) |
|
|
|
|
|
|
|
|
freq1, freq2, freq3 = 261.63, 329.63, 392.00 |
|
|
audio = (np.sin(2 * np.pi * freq1 * t) * 0.3 + |
|
|
np.sin(2 * np.pi * freq2 * t) * 0.2 + |
|
|
np.sin(2 * np.pi * freq3 * t) * 0.2) |
|
|
|
|
|
|
|
|
fade_samples = int(0.5 * sample_rate) |
|
|
audio[:fade_samples] *= np.linspace(0, 1, fade_samples) |
|
|
audio[-fade_samples:] *= np.linspace(1, 0, fade_samples) |
|
|
|
|
|
print(f"β
Audio created: {len(audio)} samples at {sample_rate}Hz") |
|
|
return audio.astype(np.float32), sample_rate |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Audio creation failed: {e}") |
|
|
return None, None |
|
|
|
|
|
def process_image_safe(self, image): |
|
|
"""Ultra-safe image processing""" |
|
|
print("π Starting SAFE image processing...") |
|
|
|
|
|
if image is None: |
|
|
return None, "β No image provided", None, None |
|
|
|
|
|
try: |
|
|
|
|
|
print("πΈ Step 1: Testing image...") |
|
|
img_info = f"Image type: {type(image)}" |
|
|
if hasattr(image, 'size'): |
|
|
img_info += f", Size: {image.size}" |
|
|
print(img_info) |
|
|
|
|
|
|
|
|
print("π Step 2: Creating description...") |
|
|
description, music_prompt = self.create_simple_description(image) |
|
|
|
|
|
|
|
|
print("π§ͺ Step 3: Testing strings...") |
|
|
desc_test = self.safe_string_test(description) |
|
|
prompt_test = self.safe_string_test(music_prompt) |
|
|
|
|
|
|
|
|
print("π΅ Step 4: Creating audio...") |
|
|
audio_data, sample_rate = self.create_simple_audio(10) |
|
|
|
|
|
|
|
|
audio_file = None |
|
|
if audio_data is not None and IMPORTS['soundfile']: |
|
|
try: |
|
|
print("πΎ Step 5: Saving audio...") |
|
|
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f: |
|
|
sf.write(f.name, audio_data, sample_rate) |
|
|
audio_file = f.name |
|
|
print(f"β
Audio saved: {audio_file}") |
|
|
except Exception as e: |
|
|
print(f"β οΈ Audio save failed: {e}") |
|
|
|
|
|
|
|
|
status_msg = f""" |
|
|
**β
SAFE MODE PROCESSING COMPLETE** |
|
|
|
|
|
**Image Analysis:** |
|
|
- {img_info} |
|
|
|
|
|
**Generated Text:** |
|
|
- Description: "{description}" |
|
|
- Music Prompt: "{music_prompt}" |
|
|
|
|
|
**String Tests:** |
|
|
- Description test: {desc_test[:100]}... |
|
|
- Prompt test: {prompt_test[:100]}... |
|
|
|
|
|
**Audio:** |
|
|
- Status: {'β
Created' if audio_data is not None else 'β Failed'} |
|
|
- Duration: {len(audio_data)/sample_rate:.1f}s if audio_data else 'N/A' |
|
|
|
|
|
**System Status:** |
|
|
- PyTorch: {'β
' if IMPORTS['torch'] else 'β'} |
|
|
- NumPy: {'β
' if IMPORTS['numpy'] else 'β'} |
|
|
- PIL: {'β
' if IMPORTS['PIL'] else 'β'} |
|
|
- SoundFile: {'β
' if IMPORTS['soundfile'] else 'β'} |
|
|
- Transformers: {'β
' if IMPORTS['transformers'] else 'β'} |
|
|
""" |
|
|
|
|
|
audio_output = (sample_rate, audio_data) if audio_data is not None else None |
|
|
|
|
|
print("β
Safe processing completed successfully!") |
|
|
return None, status_msg, audio_output, audio_file |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f""" |
|
|
β **SAFE MODE ERROR** |
|
|
|
|
|
**Error:** {str(e)} |
|
|
|
|
|
**Location:** {traceback.format_exc()} |
|
|
|
|
|
**System Info:** |
|
|
- Python: {sys.version} |
|
|
- Working directory: {os.getcwd()} |
|
|
- Environment: {dict(os.environ).get('SPACE_ID', 'Local')} |
|
|
|
|
|
**Import Status:** |
|
|
{chr(10).join([f"- {k}: {'β
' if v else 'β'}" for k, v in IMPORTS.items()])} |
|
|
""" |
|
|
print(error_msg) |
|
|
return None, error_msg, None, None |
|
|
|
|
|
|
|
|
print("π¬ Initializing generator...") |
|
|
try: |
|
|
generator = SafeGenerator() |
|
|
print("β
Generator ready!") |
|
|
except Exception as e: |
|
|
print(f"β Generator failed: {e}") |
|
|
generator = None |
|
|
|
|
|
def create_minimal_interface(): |
|
|
"""Create minimal interface for debugging""" |
|
|
|
|
|
if not IMPORTS['gradio']: |
|
|
print("β Cannot create interface - Gradio not available") |
|
|
return None |
|
|
|
|
|
print("π₯οΈ Creating minimal interface...") |
|
|
|
|
|
with gr.Blocks(title="π§ͺ Ultra-Safe Debug Mode") as demo: |
|
|
|
|
|
gr.HTML(""" |
|
|
<div style="text-align: center; background: #fff3cd; padding: 20px; border-radius: 10px; margin: 20px 0;"> |
|
|
<h1>π§ͺ ULTRA-SAFE DEBUG MODE</h1> |
|
|
<p><strong>This version isolates and tests each component individually</strong></p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
|
|
|
status_html = f""" |
|
|
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 10px 0;"> |
|
|
<h3>π§ Import Status</h3> |
|
|
<ul> |
|
|
{"".join([f"<li><strong>{k}:</strong> {'π’ OK' if v else 'π΄ Failed'}</li>" for k, v in IMPORTS.items()])} |
|
|
</ul> |
|
|
</div> |
|
|
""" |
|
|
gr.HTML(status_html) |
|
|
|
|
|
with gr.Tabs(): |
|
|
|
|
|
with gr.TabItem("π§ͺ String Test"): |
|
|
with gr.Row(): |
|
|
test_input = gr.Textbox( |
|
|
value="bright colorful sunset image", |
|
|
label="Test String Input" |
|
|
) |
|
|
test_btn = gr.Button("Test String Operations") |
|
|
|
|
|
test_output = gr.Textbox( |
|
|
label="Test Results", |
|
|
lines=10 |
|
|
) |
|
|
|
|
|
if generator: |
|
|
test_btn.click( |
|
|
fn=generator.safe_string_test, |
|
|
inputs=[test_input], |
|
|
outputs=[test_output] |
|
|
) |
|
|
|
|
|
|
|
|
with gr.TabItem("πΈ Image Test"): |
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
image_input = gr.Image( |
|
|
type="pil", |
|
|
label="Upload Test Image" |
|
|
) |
|
|
process_btn = gr.Button( |
|
|
"π Process Image (Safe Mode)", |
|
|
variant="primary" |
|
|
) |
|
|
|
|
|
with gr.Column(): |
|
|
status_output = gr.Markdown( |
|
|
value="Upload an image and click process", |
|
|
label="Processing Results" |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
audio_output = gr.Audio(label="Generated Audio") |
|
|
file_output = gr.File(label="Audio File") |
|
|
|
|
|
if generator: |
|
|
process_btn.click( |
|
|
fn=generator.process_image_safe, |
|
|
inputs=[image_input], |
|
|
outputs=[gr.Video(visible=False), status_output, audio_output, file_output] |
|
|
) |
|
|
|
|
|
gr.HTML(""" |
|
|
<div style="background: #e3f2fd; padding: 15px; border-radius: 8px; margin: 20px 0;"> |
|
|
<h3>π― Debug Instructions</h3> |
|
|
<ol> |
|
|
<li><strong>String Test:</strong> Test if string operations work correctly</li> |
|
|
<li><strong>Image Test:</strong> Upload an image to see where the error occurs</li> |
|
|
<li><strong>Check Results:</strong> Look for the exact error location in the output</li> |
|
|
</ol> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("π Launching debug interface...") |
|
|
|
|
|
try: |
|
|
demo = create_minimal_interface() |
|
|
if demo is not None: |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=True |
|
|
) |
|
|
else: |
|
|
print("β Could not create interface") |
|
|
except Exception as e: |
|
|
print(f"β Launch failed: {e}") |
|
|
print(traceback.format_exc()) |
|
|
|
|
|
|
|
|
""" |
|
|
gradio==4.44.0 |
|
|
Pillow>=9.5.0 |
|
|
numpy>=1.24.0 |
|
|
soundfile>=0.12.0 |
|
|
torch>=2.0.0 |
|
|
transformers>=4.30.0 |
|
|
""" |
|
|
|
|
|
|
|
|
""" |
|
|
--- |
|
|
title: Ultra-Safe Photo Soundtrack Debug |
|
|
emoji: π§ͺ |
|
|
colorFrom: red |
|
|
colorTo: orange |
|
|
sdk: gradio |
|
|
sdk_version: "4.44.0" |
|
|
app_file: app.py |
|
|
pinned: false |
|
|
license: apache-2.0 |
|
|
--- |
|
|
|
|
|
# π§ͺ Ultra-Safe Debug Mode |
|
|
|
|
|
This is a debugging version to isolate the "bool is not iterable" error. |
|
|
|
|
|
## Features |
|
|
- β
Safe imports with individual error handling |
|
|
- β
Step-by-step processing with detailed logs |
|
|
- β
String operation testing |
|
|
- β
Fallbacks for every component |
|
|
- β
Detailed error reporting |
|
|
|
|
|
## Debug Process |
|
|
1. Upload to see import status |
|
|
2. Test string operations |
|
|
3. Process an image to find exact error location |
|
|
4. Check detailed logs for the root cause |
|
|
|
|
|
This version will work even with missing dependencies and show exactly where errors occur. |
|
|
""" |