Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import zipfile
|
| 4 |
+
|
| 5 |
+
# Define paths
|
| 6 |
+
ZIP_FILE = "nes.zip"
|
| 7 |
+
EXTRACTED_FOLDER = "nes_extracted"
|
| 8 |
+
|
| 9 |
+
# Ensure extraction directory exists
|
| 10 |
+
os.makedirs(EXTRACTED_FOLDER, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Function to extract ROMs and nested zips
|
| 13 |
+
def extract_roms():
|
| 14 |
+
if os.path.exists(ZIP_FILE):
|
| 15 |
+
# Extract the main nes.zip
|
| 16 |
+
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 17 |
+
for file in zip_ref.namelist():
|
| 18 |
+
if not file.startswith("__MACOSX/"): # Skip MacOS metadata
|
| 19 |
+
# Extract the files from nes.zip
|
| 20 |
+
zip_ref.extract(file, EXTRACTED_FOLDER)
|
| 21 |
+
# If the file is a zip (potential ROM zip), extract it too
|
| 22 |
+
if file.endswith(".zip"):
|
| 23 |
+
nested_zip_path = os.path.join(EXTRACTED_FOLDER, file)
|
| 24 |
+
extract_nested_zip(nested_zip_path)
|
| 25 |
+
else:
|
| 26 |
+
print("Error: `nes.zip` is missing. Upload it to the root directory.")
|
| 27 |
+
|
| 28 |
+
# Function to extract nested zip files (ROM zips)
|
| 29 |
+
def extract_nested_zip(nested_zip_path):
|
| 30 |
+
with zipfile.ZipFile(nested_zip_path, 'r') as nested_zip:
|
| 31 |
+
nested_folder = os.path.splitext(nested_zip_path)[0] # Get folder name from zip filename
|
| 32 |
+
os.makedirs(nested_folder, exist_ok=True)
|
| 33 |
+
nested_zip.extractall(nested_folder)
|
| 34 |
+
os.remove(nested_zip_path) # Optional: remove the nested zip after extracting
|
| 35 |
+
|
| 36 |
+
# Ensure ROMs are extracted on startup
|
| 37 |
+
extract_roms()
|
| 38 |
+
|
| 39 |
+
# Function to list available ROMs
|
| 40 |
+
def list_roms():
|
| 41 |
+
if not os.path.exists(EXTRACTED_FOLDER):
|
| 42 |
+
return []
|
| 43 |
+
# Flatten all files from subdirectories
|
| 44 |
+
files = []
|
| 45 |
+
for root, dirs, filenames in os.walk(EXTRACTED_FOLDER):
|
| 46 |
+
for filename in filenames:
|
| 47 |
+
if not filename.startswith("__MACOSX"): # Skip MacOS metadata files
|
| 48 |
+
files.append(os.path.join(root, filename))
|
| 49 |
+
return files if files else ["No ROMs available"]
|
| 50 |
+
|
| 51 |
+
# Function to return file path for download
|
| 52 |
+
def get_rom(file_name):
|
| 53 |
+
return file_name if os.path.exists(file_name) else "File not found"
|
| 54 |
+
|
| 55 |
+
# Gradio App
|
| 56 |
+
with gr.Blocks() as app:
|
| 57 |
+
gr.Markdown("# 🎮 Retro ROM File Server")
|
| 58 |
+
|
| 59 |
+
# Dropdown to select ROM
|
| 60 |
+
rom_dropdown = gr.Dropdown(choices=list_roms(), label="Select a ROM to Download", interactive=True)
|
| 61 |
+
download_button = gr.Button("Download Selected ROM")
|
| 62 |
+
file_output = gr.File(label="Download Here")
|
| 63 |
+
|
| 64 |
+
# Button Click Event
|
| 65 |
+
download_button.click(get_rom, inputs=rom_dropdown, outputs=file_output)
|
| 66 |
+
|
| 67 |
+
app.launch()
|