Spaces:
Running
Running
import gradio as gr | |
import json | |
import os | |
# File to store model data | |
MODEL_FILE = "models.json" | |
# Load existing data or create a new file if it doesn't exist | |
def load_models(): | |
if os.path.exists(MODEL_FILE): | |
with open(MODEL_FILE, "r") as f: | |
return json.load(f) | |
return [] | |
# Save data to the JSON file | |
def save_models(models): | |
with open(MODEL_FILE, "w") as f: | |
json.dump(models, f, indent=4) | |
# Function to display all models | |
def display_models(): | |
models = load_models() | |
if not models: | |
return "No models have been added yet." | |
output = "### List of AI Models:\n\n" | |
for idx, model in enumerate(models): | |
output += f"**{idx + 1}. {model['name']}**\n" | |
output += f"- **Description**: {model['description']}\n" | |
output += f"- **Year**: {model['year']}\n" | |
output += "\n" | |
return output | |
# Function to add a new model | |
def add_model(name, description, year): | |
models = load_models() | |
models.append({"name": name, "description": description, "year": year}) | |
save_models(models) | |
return "Model added successfully!", display_models() | |
# Function to edit an existing model | |
def edit_model(index, name, description, year): | |
models = load_models() | |
if index < 1 or index > len(models): | |
return "Invalid index. Please provide a valid model number.", display_models() | |
models[index - 1] = {"name": name, "description": description, "year": year} | |
save_models(models) | |
return "Model updated successfully!", display_models() | |
# Gradio interface | |
with gr.Blocks() as app: | |
gr.Markdown("# AI Model Repository\n\nAdd, view, and edit AI models contributed by the community.") | |
with gr.Tab("View Models"): | |
view_button = gr.Button("Refresh List") | |
view_output = gr.Markdown(display_models()) | |
view_button.click(display_models, outputs=view_output) | |
with gr.Tab("Add Model"): | |
with gr.Row(): | |
name_input = gr.Textbox(label="Model Name", placeholder="Enter model name") | |
year_input = gr.Textbox(label="Publication Year", placeholder="Enter year of publication") | |
description_input = gr.Textbox(label="Description", placeholder="Enter a short description") | |
add_button = gr.Button("Add Model") | |
add_output = gr.Markdown() | |
add_button.click(add_model, inputs=[name_input, description_input, year_input], outputs=[add_output, view_output]) | |
with gr.Tab("Edit Model"): | |
edit_index = gr.Number(label="Model Number", precision=0) | |
with gr.Row(): | |
edit_name = gr.Textbox(label="New Model Name", placeholder="Enter new model name") | |
edit_year = gr.Textbox(label="New Publication Year", placeholder="Enter new year of publication") | |
edit_description = gr.Textbox(label="New Description", placeholder="Enter new description") | |
edit_button = gr.Button("Edit Model") | |
edit_output = gr.Markdown() | |
edit_button.click(edit_model, inputs=[edit_index, edit_name, edit_description, edit_year], outputs=[edit_output, view_output]) | |
# Run the app | |
app.launch() | |