Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
cursor = conn.cursor()
|
| 9 |
-
cursor.execute(
|
| 10 |
CREATE TABLE IF NOT EXISTS students (
|
| 11 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 12 |
-
name TEXT
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
course TEXT NOT NULL,
|
| 16 |
-
image_path TEXT NOT NULL
|
| 17 |
)
|
| 18 |
-
|
| 19 |
conn.commit()
|
| 20 |
conn.close()
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Save the image
|
| 26 |
-
image_path = f"student_images/{email.replace('@', '_').replace('.', '_')}.png"
|
| 27 |
-
os.makedirs("student_images", exist_ok=True)
|
| 28 |
-
image.save(image_path)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
''', (name, email, phone, course, image_path))
|
| 37 |
-
conn.commit()
|
| 38 |
-
conn.close()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return f"An error occurred: {str(e)}"
|
| 45 |
|
| 46 |
# Initialize the database
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Gradio
|
| 50 |
-
|
| 51 |
-
with gr.
|
| 52 |
-
gr.Markdown("
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
app = main()
|
| 71 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sqlite3
|
| 3 |
+
from PIL import Image
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Database setup
|
| 7 |
+
DB_PATH = "students.db"
|
| 8 |
+
os.makedirs("uploads", exist_ok=True)
|
| 9 |
+
|
| 10 |
+
def setup_database():
|
| 11 |
+
conn = sqlite3.connect(DB_PATH)
|
| 12 |
cursor = conn.cursor()
|
| 13 |
+
cursor.execute("""
|
| 14 |
CREATE TABLE IF NOT EXISTS students (
|
| 15 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 16 |
+
name TEXT,
|
| 17 |
+
age INTEGER,
|
| 18 |
+
image_path TEXT
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
+
""")
|
| 21 |
conn.commit()
|
| 22 |
conn.close()
|
| 23 |
|
| 24 |
+
def register_student(name, age, image_file):
|
| 25 |
+
conn = sqlite3.connect(DB_PATH)
|
| 26 |
+
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Save image to the uploads directory
|
| 29 |
+
if image_file is not None:
|
| 30 |
+
image_path = f"uploads/{name.replace(' ', '_')}_{os.path.basename(image_file)}"
|
| 31 |
+
os.rename(image_file, image_path) # Move file to uploads directory
|
| 32 |
+
else:
|
| 33 |
+
image_path = None
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
cursor.execute("INSERT INTO students (name, age, image_path) VALUES (?, ?, ?)", (name, age, image_path))
|
| 36 |
+
conn.commit()
|
| 37 |
+
conn.close()
|
| 38 |
+
return f"Student {name} registered successfully!"
|
|
|
|
| 39 |
|
| 40 |
# Initialize the database
|
| 41 |
+
setup_database()
|
| 42 |
+
|
| 43 |
+
# Gradio Interface
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
with gr.Row():
|
| 46 |
+
gr.Markdown("### Student Registration")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
name_input = gr.Textbox(label="Name", placeholder="Enter the student's name")
|
| 50 |
+
age_input = gr.Number(label="Age", precision=0)
|
| 51 |
+
image_input = gr.Image(label="Upload Image", type="filepath")
|
| 52 |
+
|
| 53 |
+
register_button = gr.Button("Register Student")
|
| 54 |
+
output = gr.Textbox(label="Status")
|
| 55 |
+
|
| 56 |
+
register_button.click(
|
| 57 |
+
register_student,
|
| 58 |
+
inputs=[name_input, age_input, image_input],
|
| 59 |
+
outputs=output,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Launch the Gradio app
|
| 63 |
+
demo.launch()
|
|
|
|
|
|