ravinder2024 commited on
Commit
657654b
·
verified ·
1 Parent(s): 89272fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -56
app.py CHANGED
@@ -1,71 +1,63 @@
1
  import gradio as gr
2
  import sqlite3
 
3
  import os
4
 
5
- # Initialize SQLite database
6
- def init_db():
7
- conn = sqlite3.connect("students_with_images.db")
 
 
 
8
  cursor = conn.cursor()
9
- cursor.execute('''
10
  CREATE TABLE IF NOT EXISTS students (
11
  id INTEGER PRIMARY KEY AUTOINCREMENT,
12
- name TEXT NOT NULL,
13
- email TEXT NOT NULL UNIQUE,
14
- phone TEXT NOT NULL,
15
- course TEXT NOT NULL,
16
- image_path TEXT NOT NULL
17
  )
18
- ''')
19
  conn.commit()
20
  conn.close()
21
 
22
- # Function to handle student registration
23
- def register_student(name, email, phone, course, image):
24
- try:
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
- # Insert data into database
31
- conn = sqlite3.connect("students_with_images.db")
32
- cursor = conn.cursor()
33
- cursor.execute('''
34
- INSERT INTO students (name, email, phone, course, image_path)
35
- VALUES (?, ?, ?, ?, ?)
36
- ''', (name, email, phone, course, image_path))
37
- conn.commit()
38
- conn.close()
39
 
40
- return f"Student {name} successfully registered!"
41
- except sqlite3.IntegrityError:
42
- return "Error: Email already exists. Please use a different email."
43
- except Exception as e:
44
- return f"An error occurred: {str(e)}"
45
 
46
  # Initialize the database
47
- init_db()
48
-
49
- # Gradio interface
50
- def main():
51
- with gr.Blocks() as demo:
52
- gr.Markdown("## Student Registration Form")
53
-
54
- with gr.Row():
55
- name = gr.Textbox(label="Name", placeholder="Enter your full name")
56
- email = gr.Textbox(label="Email", placeholder="Enter your email address")
57
- with gr.Row():
58
- phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
59
- course = gr.Textbox(label="Course", placeholder="Enter your course name")
60
- image = gr.Image(label="Upload Image", type="file")
61
-
62
- submit_button = gr.Button("Register")
63
- output = gr.Textbox(label="Output")
64
-
65
- submit_button.click(register_student, inputs=[name, email, phone, course, image], outputs=output)
66
-
67
- return demo
68
-
69
- if __name__ == "__main__":
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()