muhammadrazapathan commited on
Commit
76e3015
·
verified ·
1 Parent(s): 3efd24d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +176 -0
app.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import sqlite3
3
+ import os
4
+ from transformers import pipeline
5
+
6
+ # -----------------------------
7
+ # LOAD LIGHT MODEL (HF SAFE)
8
+ # -----------------------------
9
+ generator = pipeline(
10
+ "text-generation",
11
+ model="distilgpt2",
12
+ device=-1 # CPU only
13
+ )
14
+
15
+ # -----------------------------
16
+ # DATABASE SETUP
17
+ # -----------------------------
18
+ DB = "college.db"
19
+
20
+ def init_db():
21
+ conn = sqlite3.connect(DB)
22
+ c = conn.cursor()
23
+
24
+ c.execute("""CREATE TABLE IF NOT EXISTS questions(
25
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
26
+ student TEXT,
27
+ year TEXT,
28
+ question TEXT,
29
+ answer TEXT
30
+ )""")
31
+
32
+ c.execute("""CREATE TABLE IF NOT EXISTS materials(
33
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
34
+ year TEXT,
35
+ subject TEXT,
36
+ filename TEXT
37
+ )""")
38
+
39
+ conn.commit()
40
+ conn.close()
41
+
42
+ init_db()
43
+
44
+ UPLOAD_DIR = "uploads"
45
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
46
+
47
+ # -----------------------------
48
+ # FUNCTIONS
49
+ # -----------------------------
50
+ def login(name, year):
51
+ if year not in ["First Year", "Second Year"]:
52
+ return "❌ Only First & Second Year Students Allowed"
53
+ return f"✅ Welcome {name}"
54
+
55
+ def generate_ai_answer(question):
56
+ prompt = f"Answer this academic question clearly:\n{question}\nAnswer:"
57
+ result = generator(prompt, max_length=150, do_sample=True)[0]["generated_text"]
58
+
59
+ # Clean output
60
+ answer = result.split("Answer:")[-1].strip()
61
+ return answer
62
+
63
+ def ask_question(name, year, question):
64
+ if year not in ["First Year", "Second Year"]:
65
+ return "Access Denied"
66
+
67
+ answer = generate_ai_answer(question)
68
+
69
+ conn = sqlite3.connect(DB)
70
+ conn.execute(
71
+ "INSERT INTO questions(student,year,question,answer) VALUES(?,?,?,?)",
72
+ (name, year, question, answer)
73
+ )
74
+ conn.commit()
75
+ conn.close()
76
+
77
+ return answer
78
+
79
+ def view_materials(year):
80
+ conn = sqlite3.connect(DB)
81
+ rows = conn.execute(
82
+ "SELECT subject, filename FROM materials WHERE year=?",
83
+ (year,)
84
+ ).fetchall()
85
+ conn.close()
86
+
87
+ if not rows:
88
+ return "No materials uploaded yet."
89
+
90
+ return "\n".join([f"{s} → {f}" for s, f in rows])
91
+
92
+ def upload_material(year, subject, file):
93
+ if file is None:
94
+ return "Upload a file"
95
+
96
+ path = os.path.join(UPLOAD_DIR, file.name)
97
+
98
+ with open(path, "wb") as f:
99
+ f.write(file.read())
100
+
101
+ conn = sqlite3.connect(DB)
102
+ conn.execute(
103
+ "INSERT INTO materials(year,subject,filename) VALUES(?,?,?)",
104
+ (year, subject, file.name)
105
+ )
106
+ conn.commit()
107
+ conn.close()
108
+
109
+ return "✅ Uploaded Successfully"
110
+
111
+ def teacher_view():
112
+ conn = sqlite3.connect(DB)
113
+ rows = conn.execute("SELECT * FROM questions").fetchall()
114
+ conn.close()
115
+
116
+ text = ""
117
+ for r in rows:
118
+ text += f"\nID {r[0]} | {r[1]} ({r[2]})\nQ: {r[3]}\nA: {r[4]}\n"
119
+ return text
120
+
121
+ def update_answer(qid, new_answer):
122
+ conn = sqlite3.connect(DB)
123
+ conn.execute(
124
+ "UPDATE questions SET answer=? WHERE id=?",
125
+ (new_answer, qid)
126
+ )
127
+ conn.commit()
128
+ conn.close()
129
+ return "Answer Updated"
130
+
131
+ # -----------------------------
132
+ # UI
133
+ # -----------------------------
134
+ with gr.Blocks(theme=gr.themes.Soft(), title="College LMS") as app:
135
+
136
+ gr.Markdown("# 🎓 AI-Powered College LMS")
137
+
138
+ with gr.Tab("Student Portal"):
139
+ name = gr.Textbox(label="Name")
140
+ year = gr.Dropdown(["First Year", "Second Year", "Other"])
141
+
142
+ login_btn = gr.Button("Login")
143
+ login_msg = gr.Textbox()
144
+ login_btn.click(login, [name, year], login_msg)
145
+
146
+ question = gr.Textbox(label="Ask Question")
147
+ ask_btn = gr.Button("Ask AI")
148
+ answer = gr.Textbox(lines=6)
149
+ ask_btn.click(ask_question, [name, year, question], answer)
150
+
151
+ mat_btn = gr.Button("View Materials")
152
+ materials = gr.Textbox()
153
+ mat_btn.click(view_materials, year, materials)
154
+
155
+ with gr.Tab("Teacher Panel"):
156
+ t_year = gr.Dropdown(["First Year", "Second Year"])
157
+ subject = gr.Textbox(label="Subject")
158
+ file = gr.File()
159
+
160
+ upload_btn = gr.Button("Upload")
161
+ upload_msg = gr.Textbox()
162
+ upload_btn.click(upload_material, [t_year, subject, file], upload_msg)
163
+
164
+ view_btn = gr.Button("Show Questions")
165
+ all_q = gr.Textbox(lines=10)
166
+ view_btn.click(teacher_view, outputs=all_q)
167
+
168
+ qid = gr.Number(label="Question ID")
169
+ new_ans = gr.Textbox(label="New Answer")
170
+
171
+ upd_btn = gr.Button("Update")
172
+ upd_msg = gr.Textbox()
173
+ upd_btn.click(update_answer, [qid, new_ans], upd_msg)
174
+
175
+ if __name__ == "__main__":
176
+ app.launch()