|
import os |
|
from flask import Flask, send_from_directory |
|
from flask_cors import CORS |
|
import gradio as gr |
|
import threading |
|
import logging |
|
|
|
|
|
os.environ["GRADIO_TEMP_DIR"] = "/tmp" |
|
|
|
|
|
flask_app = Flask(__name__) |
|
CORS(flask_app) |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
@flask_app.route("/mario") |
|
def serve_index(): |
|
return send_from_directory("/app/mario", "index.html") |
|
|
|
|
|
@flask_app.route("/mario/<path:path>") |
|
def serve_static(path): |
|
return send_from_directory("/app/mario", path) |
|
|
|
|
|
@flask_app.route("/mario/files") |
|
def list_files(): |
|
files = os.listdir("/app/mario") |
|
return {"files": files} |
|
|
|
|
|
def run_flask(): |
|
try: |
|
logging.info("Starting Flask server...") |
|
flask_app.run(host="0.0.0.0", port=5000) |
|
except Exception as e: |
|
logging.error(f"Failed to start Flask server: {e}") |
|
|
|
flask_thread = threading.Thread(target=run_flask) |
|
flask_thread.daemon = True |
|
flask_thread.start() |
|
|
|
|
|
def serve_game(): |
|
game_url = "http://localhost:5000/mario" |
|
return f'<iframe src="{game_url}" width="800" height="600" style="border: none;"></iframe>' |
|
|
|
|
|
iface = gr.Interface( |
|
fn=serve_game, |
|
inputs=None, |
|
outputs=gr.HTML(), |
|
live=True, |
|
title="Mario HTML Game", |
|
description="Play the Mario HTML game embedded in Gradio!", |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
try: |
|
logging.info("Starting Gradio interface...") |
|
iface.launch(server_name="0.0.0.0", server_port=7860) |
|
except Exception as e: |
|
logging.error(f"Failed to start Gradio interface: {e}") |