Update app.py
Browse files
app.py
CHANGED
@@ -1,167 +1,185 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import os
|
4 |
-
from dotenv import load_dotenv
|
5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
6 |
import numpy as np
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
def
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""import gradio as gr
|
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import torch
|
5 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
6 |
+
from sentence\_transformers import SentenceTransformer, util
|
7 |
import numpy as np
|
8 |
+
|
9 |
+
# ------------------------------
|
10 |
+
|
11 |
+
# Offline Quiz Generator
|
12 |
+
|
13 |
+
# ------------------------------
|
14 |
+
|
15 |
+
model\_qg = T5ForConditionalGeneration.from\_pretrained("t5-base")
|
16 |
+
tokenizer\_qg = T5Tokenizer.from\_pretrained("t5-base")
|
17 |
+
|
18 |
+
def generate\_mcqs(text, num\_questions=3):
|
19 |
+
input\_text = f"generate questions: {text}"
|
20 |
+
input\_ids = tokenizer\_qg.encode(input\_text, return\_tensors="pt", max\_length=512, truncation=True)
|
21 |
+
outputs = model\_qg.generate(input\_ids=input\_ids, max\_length=256, num\_return\_sequences=1)
|
22 |
+
return tokenizer\_qg.decode(outputs\[0], skip\_special\_tokens=True).strip()
|
23 |
+
|
24 |
+
# ------------------------------
|
25 |
+
|
26 |
+
# Weakness Analyzer
|
27 |
+
|
28 |
+
# ------------------------------
|
29 |
+
|
30 |
+
def analyze\_weakness(csv\_file):
|
31 |
+
df = pd.read\_csv(csv\_file.name)
|
32 |
+
summary = df.groupby("Topic")\["Score"].mean().sort\_values()
|
33 |
+
return summary.to\_string()
|
34 |
+
|
35 |
+
# ------------------------------
|
36 |
+
|
37 |
+
# Teaching Assistant
|
38 |
+
|
39 |
+
# ------------------------------
|
40 |
+
|
41 |
+
def chatbot\_response(message, history):
|
42 |
+
return "This is a placeholder response for now. (LLM not integrated)"
|
43 |
+
|
44 |
+
# ------------------------------
|
45 |
+
|
46 |
+
# Speech Question Solver
|
47 |
+
|
48 |
+
# ------------------------------
|
49 |
+
|
50 |
+
def speech\_answer(audio):
|
51 |
+
return "Audio to text transcription + answer generation is not included in offline version."
|
52 |
+
|
53 |
+
# ------------------------------
|
54 |
+
|
55 |
+
# PDF/YT Summarizer
|
56 |
+
|
57 |
+
# ------------------------------
|
58 |
+
|
59 |
+
def summarize\_text(text):
|
60 |
+
input\_text = f"summarize: {text.strip()}"
|
61 |
+
input\_ids = tokenizer\_qg.encode(input\_text, return\_tensors="pt", max\_length=512, truncation=True)
|
62 |
+
summary\_ids = model\_qg.generate(input\_ids, max\_length=150, min\_length=30, length\_penalty=5., num\_beams=2)
|
63 |
+
return tokenizer\_qg.decode(summary\_ids\[0], skip\_special\_tokens=True)
|
64 |
+
|
65 |
+
# ------------------------------
|
66 |
+
|
67 |
+
# Engagement Predictor (Mock)
|
68 |
+
|
69 |
+
# ------------------------------
|
70 |
+
|
71 |
+
def predict\_engagement(file):
|
72 |
+
df = pd.read\_csv(file.name)
|
73 |
+
avg\_time = df\['TimeSpent'].mean()
|
74 |
+
if avg\_time < 10:
|
75 |
+
return "β οΈ Risk of disengagement"
|
76 |
+
else:
|
77 |
+
return "β
Engaged student"
|
78 |
+
|
79 |
+
# ------------------------------
|
80 |
+
|
81 |
+
# Badge Generator
|
82 |
+
|
83 |
+
# ------------------------------
|
84 |
+
|
85 |
+
def generate\_badge(file):
|
86 |
+
df = pd.read\_csv(file.name)
|
87 |
+
avg\_score = df\['Score'].mean()
|
88 |
+
if avg\_score >= 80:
|
89 |
+
return "π
Gold Badge"
|
90 |
+
elif avg\_score >= 50:
|
91 |
+
return "π₯ Silver Badge"
|
92 |
+
else:
|
93 |
+
return "π₯ Bronze Badge"
|
94 |
+
|
95 |
+
# ------------------------------
|
96 |
+
|
97 |
+
# Translator (Mock - offline)
|
98 |
+
|
99 |
+
# ------------------------------
|
100 |
+
|
101 |
+
def translate\_text(text, target\_lang):
|
102 |
+
return f"(Translated to {target\_lang}) - This is a mock translation."
|
103 |
+
|
104 |
+
# ------------------------------
|
105 |
+
|
106 |
+
# Plagiarism Checker
|
107 |
+
|
108 |
+
# ------------------------------
|
109 |
+
|
110 |
+
model\_plag = SentenceTransformer('all-MiniLM-L6-v2')
|
111 |
+
|
112 |
+
def check\_plagiarism(text1, text2):
|
113 |
+
emb1 = model\_plag.encode(text1, convert\_to\_tensor=True)
|
114 |
+
emb2 = model\_plag.encode(text2, convert\_to\_tensor=True)
|
115 |
+
score = util.cos\_sim(emb1, emb2).item()
|
116 |
+
return f"Similarity Score: {score:.2f} - {'β οΈ Possible Plagiarism' if score > 0.8 else 'β
Looks Original'}"
|
117 |
+
|
118 |
+
# ------------------------------
|
119 |
+
|
120 |
+
# Gradio UI
|
121 |
+
|
122 |
+
# ------------------------------
|
123 |
+
|
124 |
+
with gr.Blocks() as demo:
|
125 |
+
gr.Markdown("# π AI-Powered LMS Suite (Offline Mode)")
|
126 |
+
|
127 |
+
```
|
128 |
+
with gr.Tab("π§ Quiz Generator"):
|
129 |
+
quiz_text = gr.Textbox(label="Content", lines=5)
|
130 |
+
quiz_slider = gr.Slider(1, 10, value=3, label="Number of Questions")
|
131 |
+
quiz_btn = gr.Button("Generate Quiz")
|
132 |
+
quiz_out = gr.Textbox(label="Generated Quiz")
|
133 |
+
quiz_btn.click(fn=generate_mcqs, inputs=[quiz_text, quiz_slider], outputs=quiz_out)
|
134 |
+
|
135 |
+
with gr.Tab("π Weakness Analyzer"):
|
136 |
+
weak_file = gr.File(label="Upload CSV with Topic & Score columns")
|
137 |
+
weak_btn = gr.Button("Analyze")
|
138 |
+
weak_out = gr.Textbox(label="Analysis")
|
139 |
+
weak_btn.click(fn=analyze_weakness, inputs=weak_file, outputs=weak_out)
|
140 |
+
|
141 |
+
with gr.Tab("π€ Teaching Assistant"):
|
142 |
+
chat = gr.ChatInterface(fn=chatbot_response)
|
143 |
+
|
144 |
+
with gr.Tab("π€ Speech Q Solver"):
|
145 |
+
audio_in = gr.Audio(source="microphone", type="filepath")
|
146 |
+
audio_btn = gr.Button("Answer")
|
147 |
+
audio_out = gr.Textbox()
|
148 |
+
audio_btn.click(fn=speech_answer, inputs=audio_in, outputs=audio_out)
|
149 |
+
|
150 |
+
with gr.Tab("π Summarizer"):
|
151 |
+
sum_text = gr.Textbox(lines=5, label="Paste Text")
|
152 |
+
sum_btn = gr.Button("Summarize")
|
153 |
+
sum_out = gr.Textbox(label="Summary")
|
154 |
+
sum_btn.click(fn=summarize_text, inputs=sum_text, outputs=sum_out)
|
155 |
+
|
156 |
+
with gr.Tab("π Engagement Predictor"):
|
157 |
+
eng_file = gr.File(label="Upload CSV with TimeSpent column")
|
158 |
+
eng_btn = gr.Button("Predict")
|
159 |
+
eng_out = gr.Textbox()
|
160 |
+
eng_btn.click(fn=predict_engagement, inputs=eng_file, outputs=eng_out)
|
161 |
+
|
162 |
+
with gr.Tab("π
Badge Generator"):
|
163 |
+
badge_file = gr.File(label="Upload CSV with Score column")
|
164 |
+
badge_btn = gr.Button("Get Badge")
|
165 |
+
badge_out = gr.Textbox()
|
166 |
+
badge_btn.click(fn=generate_badge, inputs=badge_file, outputs=badge_out)
|
167 |
+
|
168 |
+
with gr.Tab("π Translator"):
|
169 |
+
trans_in = gr.Textbox(label="Enter Text")
|
170 |
+
trans_lang = gr.Textbox(label="Target Language")
|
171 |
+
trans_btn = gr.Button("Translate")
|
172 |
+
trans_out = gr.Textbox()
|
173 |
+
trans_btn.click(fn=translate_text, inputs=[trans_in, trans_lang], outputs=trans_out)
|
174 |
+
|
175 |
+
with gr.Tab("π Plagiarism Checker"):
|
176 |
+
text1 = gr.Textbox(label="Text 1", lines=3)
|
177 |
+
text2 = gr.Textbox(label="Text 2", lines=3)
|
178 |
+
plag_btn = gr.Button("Check Similarity")
|
179 |
+
plag_out = gr.Textbox()
|
180 |
+
plag_btn.click(fn=check_plagiarism, inputs=[text1, text2], outputs=plag_out)
|
181 |
+
```
|
182 |
+
|
183 |
+
# Launch app
|
184 |
+
|
185 |
+
demo.launch()
|