Spaces:
Running
Running
File size: 3,100 Bytes
f24d252 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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()
|