Yao211's picture
Update app.py
acdc054 verified
import gradio as gr
import sys
import os
# Add the dist directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'dist'))
# Import obfuscated module
try:
import core_logic
from core_logic import *
data_manager = DataManager()
except ImportError as e:
print(f"Error: Obfuscated module not found: {e}")
print("Current directory:", os.getcwd())
print("Files in dist:", os.listdir('dist') if os.path.exists('dist') else 'dist not found')
sys.exit(1)
def generate_speedpainting(image, request: gr.Request):
if not image:
raise gr.Error("Please upload an image first!")
user_context = get_user_context(request)
print(f"User context: {user_context}")
current_attempts = data_manager.get_attempts(user_context)
print(f"Current attempts for {user_context}: {current_attempts}")
if current_attempts >= MAX_FREE_TRIALS:
raise gr.Error(
f"You've used {MAX_FREE_TRIALS} free daily credits. "
f"Please visit https://miragic.ai/ to sign up for unlimited access!"
)
try:
# Process the image
video_url = process_media_request(image)
# Increment attempts after successful generation
new_count = data_manager.increment_attempts(user_context)
print(f"New attempt count for {user_context}: {new_count}")
return video_url
except Exception as e:
print(f"Error in generate_speedpainting: {str(e)}")
raise e
def create_footer():
return f"""
<div style="text-align: center; margin-top: 30px; padding: 15px; border-radius: 8px;">
<p style="margin: 5px 0;">Β© 2025 Miragic. All rights reserved.</p>
<p style="margin: 5px 0;">
<a href="https://www.miragic.ai" target="_blank" style="color: #0984e3; text-decoration: none;">Website</a> |
<a href="mailto:[email protected]" style="color: #0984e3; text-decoration: none;">Contact Us</a> |
<a href="https://www.miragic.ai/privacy-policy" target="_blank" style="color: #0984e3; text-decoration: none;">Privacy Policy</a>
</p>
</div>
"""
# Example images
EXAMPLE_IMAGES = [
["static/assets/1.jpg"],
["static/assets/2.jpg"],
["static/assets/3.jpg"],
["static/assets/4.jpg"],
["static/assets/5.jpg"],
]
# Company information
COMPANY_INFO = """
## About US
### Miragic is a cutting-edge platform to serve AI-powered tools like Virtual Try-on, Speed Painting, Background Remover and Sales Pilot.
### [Visit our website](https://miragic.ai) to explore more innovative generative AI tools!
"""
# Custom CSS
css = """
footer {visibility: hidden}
.banner {
background-color: #f8f9fa;
padding: 10px;
border-radius: 5px;
margin-bottom: 20px;
text-align: center;
}
.button-gradient {
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
background-size: 400% 400%;
border: none;
padding: 14px 28px;
font-size: 16px;
font-weight: bold;
color: white;
border-radius: 10px;
cursor: pointer;
transition: 0.3s ease-in-out;
animation: gradientAnimation 2s infinite linear;
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
}
@keyframes gradientAnimation {
0% { background-position: 0% 50%; }
100% { background-position: 100% 50%; }
}
.button-gradient:hover {
transform: scale(1.05);
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
}
.signup-container {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 8px;
color: white;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.signup-container h3 {
margin-bottom: 10px;
color: white;
}
.signup-container p {
margin-bottom: 15px;
color: #f0f0f0;
}
.signup-button {
background: linear-gradient(45deg, #ff416c, #ff4b2b);
border: none;
padding: 12px 25px;
font-size: 16px;
font-weight: bold;
color: white;
border-radius: 8px;
text-decoration: none;
display: inline-block;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.signup-button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.attempts-counter {
background: #e3f2fd;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
text-align: center;
font-weight: bold;
color: #1976d2;
}
.interaction-section {
text-align: center;
padding: 10px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 8px;
color: white;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
"""
# Create Gradio interface
with gr.Blocks(title="Miragic Speed-Painting", theme=gr.themes.Ocean(), css=css) as demo:
gr.Markdown("""
<div style="display: flex; align-items: center;">
<img src="https://avatars.githubusercontent.com/u/211682198?s=200&v=4" style="width: 80px; margin-right: 20px;"/>
<div>
<h1 style="margin-bottom: 0;">Miragic Speed-Painting 🎨</h1>
<p>Upload an image to see AI create speedpainting animations!</p>
</div>
</div>
""")
gr.Markdown(COMPANY_INFO)
with gr.Row():
with gr.Column():
image_input = gr.Image(
label="Upload Image",
type="pil",
sources=["upload", "clipboard"],
height=300
)
gr.Examples(
examples=EXAMPLE_IMAGES,
inputs=image_input,
label="Try these examples!",
examples_per_page=5
)
submit_btn = gr.Button("Generate Speedpainting πŸš€", elem_classes="button-gradient")
with gr.Column():
video_output = gr.Video(
label="Speedpainting Result",
autoplay=True,
height=300
)
gr.HTML("""
<div class="interaction-section">
<p style="margin: 5px 0;">If you like our Speed Painting results, please give us a ⭐ into our space!</p>
</div>
""")
signup_prompt = gr.HTML(
visible=True,
value="""<div class="signup-container">
<h3>πŸš€ Want unlimited generations?</h3>
<p>Please sign up at Miragic.ai for unlimited access to all our AI tools!</p>
<a href='https://miragic.ai/products/speed-painting' target='_blank' class="signup-button">
SignUp for Free πŸš€
</a>
</div>"""
)
# Handle generation
submit_btn.click(
fn=generate_speedpainting,
inputs=[image_input],
outputs=video_output
)
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiragic-AI%2FMiragic-Speed-Painting"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiragic-AI%2FMiragic-Speed-Painting&label=VISITORS&labelColor=%2337d67a&countColor=%23f47373&style=plastic&labelStyle=upper" /></a>')
# Footer
gr.HTML(create_footer())
if __name__ == "__main__":
demo.launch()