Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,8 @@ init_db()
|
|
| 29 |
|
| 30 |
# 🔹 Load YAML into DB
|
| 31 |
def load_yaml_to_db():
|
|
|
|
|
|
|
| 32 |
with open(YAML_FILE, 'r', encoding='utf-8') as f:
|
| 33 |
data = yaml.safe_load(f)
|
| 34 |
|
|
@@ -47,9 +49,40 @@ def load_yaml_to_db():
|
|
| 47 |
|
| 48 |
conn.commit()
|
| 49 |
conn.close()
|
| 50 |
-
return "✅ YAML loaded
|
| 51 |
|
| 52 |
-
# 🔹
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def search_items(keyword):
|
| 54 |
conn = sqlite3.connect(DB_FILE)
|
| 55 |
query = """
|
|
@@ -60,57 +93,114 @@ def search_items(keyword):
|
|
| 60 |
conn.close()
|
| 61 |
return df
|
| 62 |
|
| 63 |
-
# 🔹 Get
|
| 64 |
-
def
|
| 65 |
conn = sqlite3.connect(DB_FILE)
|
| 66 |
cursor = conn.cursor()
|
| 67 |
cursor.execute("SELECT * FROM items WHERE id=?", (item_id,))
|
| 68 |
row = cursor.fetchone()
|
| 69 |
conn.close()
|
| 70 |
if row:
|
| 71 |
-
|
| 72 |
-
return {
|
| 73 |
-
"Name (EN)": row[1],
|
| 74 |
-
"Name (BN)": row[2],
|
| 75 |
-
"Description (EN)": row[3],
|
| 76 |
-
"Description (BN)": row[4],
|
| 77 |
-
"Photo": row[5],
|
| 78 |
-
"Voice": row[6]
|
| 79 |
-
}
|
| 80 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
return "❌ Item not found"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# 🔹 Gradio UI
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
-
gr.Markdown("## 🐄 Multi-language Media Database
|
| 86 |
|
| 87 |
with gr.Tab("Load YAML"):
|
| 88 |
-
load_btn = gr.Button("Load YAML
|
| 89 |
load_output = gr.Textbox(label="Status")
|
| 90 |
load_btn.click(load_yaml_to_db, inputs=[], outputs=load_output)
|
| 91 |
|
| 92 |
-
with gr.Tab("
|
| 93 |
-
search_input = gr.Textbox(label="Search (
|
| 94 |
search_btn = gr.Button("Search")
|
| 95 |
search_output = gr.Dataframe()
|
|
|
|
|
|
|
| 96 |
search_btn.click(search_items, inputs=search_input, outputs=search_output)
|
|
|
|
| 97 |
|
| 98 |
-
with gr.Tab("
|
| 99 |
item_id_input = gr.Number(label="Item ID")
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
|
| 116 |
demo.launch()
|
|
|
|
| 29 |
|
| 30 |
# 🔹 Load YAML into DB
|
| 31 |
def load_yaml_to_db():
|
| 32 |
+
if not os.path.exists(YAML_FILE):
|
| 33 |
+
return "❌ data.yaml not found!"
|
| 34 |
with open(YAML_FILE, 'r', encoding='utf-8') as f:
|
| 35 |
data = yaml.safe_load(f)
|
| 36 |
|
|
|
|
| 49 |
|
| 50 |
conn.commit()
|
| 51 |
conn.close()
|
| 52 |
+
return f"✅ YAML loaded: {len(data)} items"
|
| 53 |
|
| 54 |
+
# 🔹 Add Item
|
| 55 |
+
def add_item(name_en, name_bn, desc_en, desc_bn, photo, voice):
|
| 56 |
+
if not all([name_en, name_bn, desc_en, desc_bn]):
|
| 57 |
+
return "❌ All text fields are required"
|
| 58 |
+
|
| 59 |
+
# Save media files
|
| 60 |
+
photo_path, voice_path = None, None
|
| 61 |
+
if photo:
|
| 62 |
+
photo_path = f"media/photo/{os.path.basename(photo.name)}"
|
| 63 |
+
photo.save(photo_path)
|
| 64 |
+
if voice:
|
| 65 |
+
voice_path = f"media/voice/{os.path.basename(voice.name)}"
|
| 66 |
+
voice.save(voice_path)
|
| 67 |
+
|
| 68 |
+
conn = sqlite3.connect(DB_FILE)
|
| 69 |
+
cursor = conn.cursor()
|
| 70 |
+
cursor.execute("""
|
| 71 |
+
INSERT INTO items (name_en, name_bn, description_en, description_bn, photo_path, voice_path)
|
| 72 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 73 |
+
""", (name_en, name_bn, desc_en, desc_bn, photo_path, voice_path))
|
| 74 |
+
conn.commit()
|
| 75 |
+
conn.close()
|
| 76 |
+
return f"✅ Item '{name_en}' added!"
|
| 77 |
+
|
| 78 |
+
# 🔹 View All Items
|
| 79 |
+
def view_items():
|
| 80 |
+
conn = sqlite3.connect(DB_FILE)
|
| 81 |
+
df = pd.read_sql_query("SELECT * FROM items", conn)
|
| 82 |
+
conn.close()
|
| 83 |
+
return df
|
| 84 |
+
|
| 85 |
+
# 🔹 Search Items
|
| 86 |
def search_items(keyword):
|
| 87 |
conn = sqlite3.connect(DB_FILE)
|
| 88 |
query = """
|
|
|
|
| 93 |
conn.close()
|
| 94 |
return df
|
| 95 |
|
| 96 |
+
# 🔹 Get Item by ID
|
| 97 |
+
def get_item(item_id):
|
| 98 |
conn = sqlite3.connect(DB_FILE)
|
| 99 |
cursor = conn.cursor()
|
| 100 |
cursor.execute("SELECT * FROM items WHERE id=?", (item_id,))
|
| 101 |
row = cursor.fetchone()
|
| 102 |
conn.close()
|
| 103 |
if row:
|
| 104 |
+
return [row[1], row[2], row[3], row[4], row[5], row[6]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
+
return ["","","","","",None,None]
|
| 107 |
+
|
| 108 |
+
# 🔹 Update Item
|
| 109 |
+
def update_item(item_id, name_en, name_bn, desc_en, desc_bn, photo, voice):
|
| 110 |
+
conn = sqlite3.connect(DB_FILE)
|
| 111 |
+
cursor = conn.cursor()
|
| 112 |
+
|
| 113 |
+
# Existing media paths
|
| 114 |
+
cursor.execute("SELECT photo_path, voice_path FROM items WHERE id=?", (item_id,))
|
| 115 |
+
row = cursor.fetchone()
|
| 116 |
+
if not row:
|
| 117 |
return "❌ Item not found"
|
| 118 |
+
photo_path, voice_path = row
|
| 119 |
+
|
| 120 |
+
# Update media if new uploaded
|
| 121 |
+
if photo:
|
| 122 |
+
photo_path = f"media/photo/{os.path.basename(photo.name)}"
|
| 123 |
+
photo.save(photo_path)
|
| 124 |
+
if voice:
|
| 125 |
+
voice_path = f"media/voice/{os.path.basename(voice.name)}"
|
| 126 |
+
voice.save(voice_path)
|
| 127 |
+
|
| 128 |
+
cursor.execute("""
|
| 129 |
+
UPDATE items SET name_en=?, name_bn=?, description_en=?, description_bn=?, photo_path=?, voice_path=?
|
| 130 |
+
WHERE id=?
|
| 131 |
+
""", (name_en, name_bn, desc_en, desc_bn, photo_path, voice_path, item_id))
|
| 132 |
+
|
| 133 |
+
conn.commit()
|
| 134 |
+
conn.close()
|
| 135 |
+
return f"✅ Item ID {item_id} updated!"
|
| 136 |
+
|
| 137 |
+
# 🔹 Delete Item
|
| 138 |
+
def delete_item(item_id):
|
| 139 |
+
conn = sqlite3.connect(DB_FILE)
|
| 140 |
+
cursor = conn.cursor()
|
| 141 |
+
cursor.execute("DELETE FROM items WHERE id=?", (item_id,))
|
| 142 |
+
conn.commit()
|
| 143 |
+
conn.close()
|
| 144 |
+
return f"❌ Item ID {item_id} deleted!"
|
| 145 |
+
|
| 146 |
+
# 🔹 Export CSV
|
| 147 |
+
def export_csv():
|
| 148 |
+
df = view_items()
|
| 149 |
+
df.to_csv("export_items.csv", index=False)
|
| 150 |
+
return "✅ Exported to export_items.csv"
|
| 151 |
|
| 152 |
# 🔹 Gradio UI
|
| 153 |
with gr.Blocks() as demo:
|
| 154 |
+
gr.Markdown("## 🐄 Advanced Multi-language Media Database (বাংলা + English)")
|
| 155 |
|
| 156 |
with gr.Tab("Load YAML"):
|
| 157 |
+
load_btn = gr.Button("Load YAML")
|
| 158 |
load_output = gr.Textbox(label="Status")
|
| 159 |
load_btn.click(load_yaml_to_db, inputs=[], outputs=load_output)
|
| 160 |
|
| 161 |
+
with gr.Tab("View / Search"):
|
| 162 |
+
search_input = gr.Textbox(label="Search (বাংলা/English)")
|
| 163 |
search_btn = gr.Button("Search")
|
| 164 |
search_output = gr.Dataframe()
|
| 165 |
+
view_btn = gr.Button("View All Items")
|
| 166 |
+
view_output = gr.Dataframe()
|
| 167 |
search_btn.click(search_items, inputs=search_input, outputs=search_output)
|
| 168 |
+
view_btn.click(view_items, inputs=[], outputs=view_output)
|
| 169 |
|
| 170 |
+
with gr.Tab("Media Gallery / CRUD"):
|
| 171 |
item_id_input = gr.Number(label="Item ID")
|
| 172 |
+
name_en_input = gr.Textbox(label="Name (EN)")
|
| 173 |
+
name_bn_input = gr.Textbox(label="Name (BN)")
|
| 174 |
+
desc_en_input = gr.Textbox(label="Description (EN)")
|
| 175 |
+
desc_bn_input = gr.Textbox(label="Description (BN)")
|
| 176 |
+
photo_input = gr.File(label="Photo", file_types=[".png",".jpg",".jpeg"])
|
| 177 |
+
voice_input = gr.File(label="Voice", file_types=[".mp3",".wav"])
|
| 178 |
+
|
| 179 |
+
# Display
|
| 180 |
+
display_btn = gr.Button("Load Item")
|
| 181 |
+
display_name_en = gr.Textbox(label="Name (EN)")
|
| 182 |
+
display_name_bn = gr.Textbox(label="Name (BN)")
|
| 183 |
+
display_desc_en = gr.Textbox(label="Description (EN)")
|
| 184 |
+
display_desc_bn = gr.Textbox(label="Description (BN)")
|
| 185 |
+
display_photo = gr.Image(label="Photo")
|
| 186 |
+
display_voice = gr.Audio(label="Voice")
|
| 187 |
+
display_btn.click(get_item, inputs=item_id_input, outputs=[display_name_en, display_name_bn, display_desc_en, display_desc_bn, display_photo, display_voice])
|
| 188 |
|
| 189 |
+
# CRUD buttons
|
| 190 |
+
add_btn = gr.Button("Add / Save New Item")
|
| 191 |
+
add_output = gr.Textbox(label="Status")
|
| 192 |
+
add_btn.click(add_item, inputs=[name_en_input,name_bn_input,desc_en_input,desc_bn_input,photo_input,voice_input], outputs=add_output)
|
| 193 |
+
|
| 194 |
+
update_btn = gr.Button("Update Item")
|
| 195 |
+
update_output = gr.Textbox(label="Status")
|
| 196 |
+
update_btn.click(update_item, inputs=[item_id_input,name_en_input,name_bn_input,desc_en_input,desc_bn_input,photo_input,voice_input], outputs=update_output)
|
| 197 |
+
|
| 198 |
+
delete_btn = gr.Button("Delete Item")
|
| 199 |
+
delete_output = gr.Textbox(label="Status")
|
| 200 |
+
delete_btn.click(delete_item, inputs=item_id_input, outputs=delete_output)
|
| 201 |
|
| 202 |
+
export_btn = gr.Button("Export CSV")
|
| 203 |
+
export_output = gr.Textbox(label="Status")
|
| 204 |
+
export_btn.click(export_csv, inputs=[], outputs=export_output)
|
| 205 |
|
| 206 |
demo.launch()
|