sathwikabhavaraju2005 commited on
Commit
e8c442a
Β·
verified Β·
1 Parent(s): 3576625

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -138
app.py CHANGED
@@ -1,185 +1,152 @@
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()
 
1
  import gradio as gr
2
  import pandas as pd
 
3
  import torch
4
  from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+ from sentence_transformers import SentenceTransformer, util
 
6
 
7
  # ------------------------------
 
8
  # Offline Quiz Generator
 
9
  # ------------------------------
10
+ model_qg = T5ForConditionalGeneration.from_pretrained("t5-base")
11
+ tokenizer_qg = T5Tokenizer.from_pretrained("t5-base")
12
 
13
+ def generate_mcqs(text, num_questions=3):
14
+ input_text = f"generate questions: {text}"
15
+ input_ids = tokenizer_qg.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
16
+ outputs = model_qg.generate(input_ids=input_ids, max_length=256, num_return_sequences=1)
17
+ return tokenizer_qg.decode(outputs[0], skip_special_tokens=True).strip()
 
 
 
18
 
19
  # ------------------------------
 
20
  # Weakness Analyzer
 
21
  # ------------------------------
22
+ def analyze_weakness(csv_file):
23
+ df = pd.read_csv(csv_file.name)
24
+ summary = df.groupby("Topic")["Score"].mean().sort_values()
25
+ return summary.to_string()
 
26
 
27
  # ------------------------------
 
28
  # Teaching Assistant
 
29
  # ------------------------------
30
+ def chatbot_response(message, history):
31
+ return "This is a placeholder response for now. (LLM not integrated)"
 
32
 
33
  # ------------------------------
 
34
  # Speech Question Solver
 
35
  # ------------------------------
36
+ def speech_answer(audio):
37
+ return "Audio to text transcription + answer generation is not included in offline version."
 
38
 
39
  # ------------------------------
 
40
  # PDF/YT Summarizer
 
41
  # ------------------------------
42
+ def summarize_text(text):
43
+ input_text = f"summarize: {text.strip()}"
44
+ input_ids = tokenizer_qg.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
45
+ summary_ids = model_qg.generate(input_ids, max_length=150, min_length=30, length_penalty=5., num_beams=2)
46
+ return tokenizer_qg.decode(summary_ids[0], skip_special_tokens=True)
 
47
 
48
  # ------------------------------
 
49
  # Engagement Predictor (Mock)
 
50
  # ------------------------------
51
+ def predict_engagement(file):
52
+ df = pd.read_csv(file.name)
53
+ avg_time = df['TimeSpent'].mean()
54
+ if avg_time < 10:
55
+ return "⚠️ Risk of disengagement"
56
+ else:
57
+ return "βœ… Engaged student"
 
58
 
59
  # ------------------------------
 
60
  # Badge Generator
 
61
  # ------------------------------
62
+ def generate_badge(file):
63
+ df = pd.read_csv(file.name)
64
+ avg_score = df['Score'].mean()
65
+ if avg_score >= 80:
66
+ return "πŸ… Gold Badge"
67
+ elif avg_score >= 50:
68
+ return "πŸ₯ˆ Silver Badge"
69
+ else:
70
+ return "πŸ₯‰ Bronze Badge"
 
71
 
72
  # ------------------------------
73
+ # Translator (Mock)
 
 
74
  # ------------------------------
75
+ def translate_text(text, target_lang):
76
+ return f"(Translated to {target_lang}) - This is a mock translation."
 
77
 
78
  # ------------------------------
 
79
  # Plagiarism Checker
 
80
  # ------------------------------
81
+ model_plag = SentenceTransformer('all-MiniLM-L6-v2')
82
 
83
+ def check_plagiarism(text1, text2):
84
+ emb1 = model_plag.encode(text1, convert_to_tensor=True)
85
+ emb2 = model_plag.encode(text2, convert_to_tensor=True)
86
+ score = util.cos_sim(emb1, emb2).item()
87
+ return f"Similarity Score: {score:.2f} - {'⚠️ Possible Plagiarism' if score > 0.8 else 'βœ… Looks Original'}"
 
 
88
 
89
  # ------------------------------
 
90
  # Gradio UI
 
91
  # ------------------------------
 
92
  with gr.Blocks() as demo:
93
+ gr.Markdown("# πŸ“š AI-Powered LMS Suite (Offline Mode)")
94
+
95
+ with gr.Tab("🧠 Quiz Generator"):
96
+ quiz_text = gr.Textbox(label="Content", lines=5)
97
+ quiz_slider = gr.Slider(1, 10, value=3, label="Number of Questions")
98
+ quiz_btn = gr.Button("Generate Quiz")
99
+ quiz_out = gr.Textbox(label="Generated Quiz")
100
+ quiz_btn.click(fn=generate_mcqs, inputs=[quiz_text, quiz_slider], outputs=quiz_out)
101
+
102
+ with gr.Tab("πŸ“‰ Weakness Analyzer"):
103
+ weak_file = gr.File(label="Upload CSV with Topic & Score columns")
104
+ weak_btn = gr.Button("Analyze")
105
+ weak_out = gr.Textbox(label="Analysis")
106
+ weak_btn.click(fn=analyze_weakness, inputs=weak_file, outputs=weak_out)
107
+
108
+ with gr.Tab("πŸ€– Teaching Assistant"):
109
+ gr.ChatInterface(fn=chatbot_response)
110
+
111
+ with gr.Tab("🎀 Speech Q Solver"):
112
+ audio_in = gr.Audio(source="microphone", type="filepath")
113
+ audio_btn = gr.Button("Answer")
114
+ audio_out = gr.Textbox()
115
+ audio_btn.click(fn=speech_answer, inputs=audio_in, outputs=audio_out)
116
+
117
+ with gr.Tab("πŸ“„ Summarizer"):
118
+ sum_text = gr.Textbox(lines=5, label="Paste Text")
119
+ sum_btn = gr.Button("Summarize")
120
+ sum_out = gr.Textbox(label="Summary")
121
+ sum_btn.click(fn=summarize_text, inputs=sum_text, outputs=sum_out)
122
+
123
+ with gr.Tab("πŸ“Š Engagement Predictor"):
124
+ eng_file = gr.File(label="Upload CSV with TimeSpent column")
125
+ eng_btn = gr.Button("Predict")
126
+ eng_out = gr.Textbox()
127
+ eng_btn.click(fn=predict_engagement, inputs=eng_file, outputs=eng_out)
128
+
129
+ with gr.Tab("πŸ… Badge Generator"):
130
+ badge_file = gr.File(label="Upload CSV with Score column")
131
+ badge_btn = gr.Button("Get Badge")
132
+ badge_out = gr.Textbox()
133
+ badge_btn.click(fn=generate_badge, inputs=badge_file, outputs=badge_out)
134
+
135
+ with gr.Tab("🌍 Translator"):
136
+ trans_in = gr.Textbox(label="Enter Text")
137
+ trans_lang = gr.Textbox(label="Target Language")
138
+ trans_btn = gr.Button("Translate")
139
+ trans_out = gr.Textbox()
140
+ trans_btn.click(fn=translate_text, inputs=[trans_in, trans_lang], outputs=trans_out)
141
+
142
+ with gr.Tab("πŸ“‹ Plagiarism Checker"):
143
+ text1 = gr.Textbox(label="Text 1", lines=3)
144
+ text2 = gr.Textbox(label="Text 2", lines=3)
145
+ plag_btn = gr.Button("Check Similarity")
146
+ plag_out = gr.Textbox()
147
+ plag_btn.click(fn=check_plagiarism, inputs=[text1, text2], outputs=plag_out)
148
+
149
+ # ------------------------------
150
+ # Launch App
151
+ # ------------------------------
 
152
  demo.launch()