Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,176 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import sqlite3
|
| 3 |
-
import os
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
|
| 6 |
-
# -----------------------------
|
| 7 |
-
# LOAD MODEL (CPU SAFE)
|
| 8 |
-
# -----------------------------
|
| 9 |
-
qa_pipeline = pipeline(
|
| 10 |
-
"text2text-generation",
|
| 11 |
-
model="google/flan-t5-base",
|
| 12 |
-
device=-1 # force CPU (important for Spaces)
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# -----------------------------
|
| 16 |
-
# DATABASE
|
| 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 Allowed"
|
| 53 |
-
return f"✅ Welcome {name}"
|
| 54 |
-
|
| 55 |
-
def generate_ai_answer(question):
|
| 56 |
-
result = qa_pipeline(
|
| 57 |
-
f"Explain simply for a student: {question}",
|
| 58 |
-
max_length=200
|
| 59 |
-
)[0]["generated_text"]
|
| 60 |
-
return result
|
| 61 |
-
|
| 62 |
-
def ask_question(name, year, question):
|
| 63 |
-
if year not in ["First Year", "Second Year"]:
|
| 64 |
-
return "Access Denied"
|
| 65 |
-
|
| 66 |
-
answer = generate_ai_answer(question)
|
| 67 |
-
|
| 68 |
-
conn = sqlite3.connect(DB)
|
| 69 |
-
conn.execute(
|
| 70 |
-
"INSERT INTO questions(student,year,question,answer) VALUES(?,?,?,?)",
|
| 71 |
-
(name, year, question, answer)
|
| 72 |
-
)
|
| 73 |
-
conn.commit()
|
| 74 |
-
conn.close()
|
| 75 |
-
|
| 76 |
-
return answer
|
| 77 |
-
|
| 78 |
-
def view_materials(year):
|
| 79 |
-
conn = sqlite3.connect(DB)
|
| 80 |
-
rows = conn.execute(
|
| 81 |
-
"SELECT subject, filename FROM materials WHERE year=?",
|
| 82 |
-
(year,)
|
| 83 |
-
).fetchall()
|
| 84 |
-
conn.close()
|
| 85 |
-
|
| 86 |
-
if not rows:
|
| 87 |
-
return "No materials uploaded yet."
|
| 88 |
-
|
| 89 |
-
return "\n".join([f"{s} → {f}" for s, f in rows])
|
| 90 |
-
|
| 91 |
-
def upload_material(year, subject, file):
|
| 92 |
-
if file is None:
|
| 93 |
-
return "Upload a file"
|
| 94 |
-
|
| 95 |
-
path = os.path.join(UPLOAD_DIR, file.name)
|
| 96 |
-
|
| 97 |
-
with open(path, "wb") as f:
|
| 98 |
-
f.write(file.read())
|
| 99 |
-
|
| 100 |
-
conn = sqlite3.connect(DB)
|
| 101 |
-
conn.execute(
|
| 102 |
-
"INSERT INTO materials(year,subject,filename) VALUES(?,?,?)",
|
| 103 |
-
(year, subject, file.name)
|
| 104 |
-
)
|
| 105 |
-
conn.commit()
|
| 106 |
-
conn.close()
|
| 107 |
-
|
| 108 |
-
return "Uploaded Successfully"
|
| 109 |
-
|
| 110 |
-
def teacher_view():
|
| 111 |
-
conn = sqlite3.connect(DB)
|
| 112 |
-
rows = conn.execute("SELECT * FROM questions").fetchall()
|
| 113 |
-
conn.close()
|
| 114 |
-
|
| 115 |
-
text = ""
|
| 116 |
-
for r in rows:
|
| 117 |
-
text += f"\nID {r[0]} | {r[1]} ({r[2]})\nQ: {r[3]}\nA: {r[4]}\n"
|
| 118 |
-
return text
|
| 119 |
-
|
| 120 |
-
def update_answer(qid, new_answer):
|
| 121 |
-
conn = sqlite3.connect(DB)
|
| 122 |
-
conn.execute(
|
| 123 |
-
"UPDATE questions SET answer=? WHERE id=?",
|
| 124 |
-
(new_answer, qid)
|
| 125 |
-
)
|
| 126 |
-
conn.commit()
|
| 127 |
-
conn.close()
|
| 128 |
-
return "Answer Updated"
|
| 129 |
-
|
| 130 |
-
# -----------------------------
|
| 131 |
-
# UI (Gradio v6)
|
| 132 |
-
# -----------------------------
|
| 133 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="College LMS") as app:
|
| 134 |
-
|
| 135 |
-
gr.Markdown("# 🎓 AI College LMS")
|
| 136 |
-
|
| 137 |
-
with gr.Tab("Student"):
|
| 138 |
-
name = gr.Textbox(label="Name")
|
| 139 |
-
year = gr.Dropdown(["First Year", "Second Year", "Other"])
|
| 140 |
-
|
| 141 |
-
login_btn = gr.Button("Login")
|
| 142 |
-
login_msg = gr.Textbox()
|
| 143 |
-
login_btn.click(login, [name, year], login_msg)
|
| 144 |
-
|
| 145 |
-
question = gr.Textbox(label="Ask Question")
|
| 146 |
-
ask_btn = gr.Button("Ask AI")
|
| 147 |
-
answer = gr.Textbox(lines=6)
|
| 148 |
-
ask_btn.click(ask_question, [name, year, question], answer)
|
| 149 |
-
|
| 150 |
-
mat_btn = gr.Button("View Materials")
|
| 151 |
-
materials = gr.Textbox()
|
| 152 |
-
mat_btn.click(view_materials, year, materials)
|
| 153 |
-
|
| 154 |
-
with gr.Tab("Teacher"):
|
| 155 |
-
t_year = gr.Dropdown(["First Year", "Second Year"])
|
| 156 |
-
subject = gr.Textbox(label="Subject")
|
| 157 |
-
file = gr.File()
|
| 158 |
-
|
| 159 |
-
upload_btn = gr.Button("Upload")
|
| 160 |
-
upload_msg = gr.Textbox()
|
| 161 |
-
upload_btn.click(upload_material, [t_year, subject, file], upload_msg)
|
| 162 |
-
|
| 163 |
-
view_btn = gr.Button("Show Questions")
|
| 164 |
-
all_q = gr.Textbox(lines=10)
|
| 165 |
-
view_btn.click(teacher_view, outputs=all_q)
|
| 166 |
-
|
| 167 |
-
qid = gr.Number(label="Question ID")
|
| 168 |
-
new_ans = gr.Textbox(label="New Answer")
|
| 169 |
-
|
| 170 |
-
upd_btn = gr.Button("Update")
|
| 171 |
-
upd_msg = gr.Textbox()
|
| 172 |
-
upd_btn.click(update_answer, [qid, new_ans], upd_msg)
|
| 173 |
-
|
| 174 |
-
# REQUIRED for HuggingFace
|
| 175 |
-
if __name__ == "__main__":
|
| 176 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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