File size: 6,640 Bytes
3576625
f72c672
 
a6a7a20
f72c672
c3d6c3e
 
f72c672
c3d6c3e
a6a7a20
 
 
f72c672
a6a7a20
c3d6c3e
d7f01d3
a6a7a20
d7f01d3
e8c442a
1a16e5f
a6a7a20
d7f01d3
a6a7a20
649af31
1a16e5f
649af31
1a16e5f
 
 
649af31
d7f01d3
a6a7a20
1a16e5f
c3d6c3e
f72c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6a7a20
f72c672
a6a7a20
 
 
 
 
 
 
f72c672
 
 
 
7ae9ca8
 
 
 
f72c672
41694ed
 
f72c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3d6c3e
436047e
c3d6c3e
 
a6a7a20
f72c672
 
1a16e5f
 
 
f72c672
 
e8c442a
f72c672
 
 
 
 
e8c442a
f72c672
 
e8c442a
f72c672
 
a6a7a20
f72c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6a7a20
f72c672
1e691eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
168
169
170
171
import gradio as gr
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from sentence_transformers import SentenceTransformer, util

# ------------------------------
# Load Models
# ------------------------------
qg_model_name = "iarfmoose/t5-base-question-generator"
tokenizer_qg = AutoTokenizer.from_pretrained(qg_model_name)
model_qg = AutoModelForSeq2SeqLM.from_pretrained(qg_model_name)
model_plag = SentenceTransformer('all-MiniLM-L6-v2')
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")

# ------------------------------
# Quiz Generator
# ------------------------------
def generate_mcqs(text, num_questions=3):
    input_text = f"generate questions: {text.strip()}"
    input_ids = tokenizer_qg.encode(input_text, return_tensors="pt", max_length=512, truncation=True)

    outputs = model_qg.generate(
        input_ids=input_ids,
        max_length=256,
        num_return_sequences=num_questions,
        do_sample=True,
        top_k=50,
        top_p=0.95
    )

    questions = [tokenizer_qg.decode(out, skip_special_tokens=True).strip() for out in outputs]
    return "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])

# ------------------------------
# Weakness Analyzer
# ------------------------------
def analyze_weakness(csv_file):
    df = pd.read_csv(csv_file.name)
    summary = df.groupby("Topic")["Score"].mean().sort_values()
    return summary.to_string()

# ------------------------------
# Teaching Assistant (Mock)
# ------------------------------
def chatbot_response(message, history):
    return "This is a placeholder response for now. (LLM not integrated)"

# ------------------------------
# Speech Question Solver (NEW)
# ------------------------------
def speech_answer(audio_file_path):
    transcription = asr(audio_file_path)["text"]
    input_text = f"generate questions: {transcription.strip()}"
    input_ids = tokenizer_qg.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
    outputs = model_qg.generate(input_ids, max_length=256, num_return_sequences=1)
    response = tokenizer_qg.decode(outputs[0], skip_special_tokens=True)
    return f"πŸ—£οΈ Transcript: {transcription.strip()}\n\nπŸ’‘ Answer: {response.strip()}"

# ------------------------------
# Summarizer
# ------------------------------
from transformers import pipeline

summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def summarize_text(text):
    result = summarizer(text, max_length=120, min_length=40, do_sample=False)
    return result[0]["summary_text"]

# ------------------------------
# Engagement Predictor (Mock)
# ------------------------------
def predict_engagement(file):
    df = pd.read_csv(file.name)
    avg_time = df["TimeSpent"].mean()
    return "βœ… Engaged student" if avg_time >= 10 else "⚠️ Risk of disengagement"

# ------------------------------
# Badge Generator
# ------------------------------
def generate_badge(file):
    df = pd.read_csv(file.name)
    avg_score = df["Score"].mean()
    if avg_score >= 80:
        return "πŸ… Gold Badge"
    elif avg_score >= 50:
        return "πŸ₯ˆ Silver Badge"
    else:
        return "πŸ₯‰ Bronze Badge"

# ------------------------------
# Translator (Mock)
# ------------------------------
def translate_text(text, target_lang):
    return f"(Translated to {target_lang}) - This is a mock translation."

# ------------------------------
# Plagiarism Checker
# ------------------------------
def check_plagiarism(text1, text2):
    emb1 = model_plag.encode(text1, convert_to_tensor=True)
    emb2 = model_plag.encode(text2, convert_to_tensor=True)
    score = util.cos_sim(emb1, emb2).item()
    return f"Similarity Score: {score:.2f} - {'⚠️ Possible Plagiarism' if score > 0.8 else 'βœ… Looks Original'}"

# ------------------------------
# Gradio Interface
# ------------------------------
with gr.Blocks() as demo:
    gr.Markdown("# πŸ“š Smart LMS Suite (Offline)")

    with gr.Tab("🧠 Quiz Generator"):
        quiz_text = gr.Textbox(label="πŸ“„ Input Content", lines=6, placeholder="Paste a paragraph here...")
        quiz_slider = gr.Slider(1, 10, value=3, label="🧾 Number of Questions")
        quiz_btn = gr.Button("πŸš€ Generate Quiz")
        quiz_output = gr.Textbox(label="πŸ“‹ Generated Questions", lines=10)
        quiz_btn.click(fn=generate_mcqs, inputs=[quiz_text, quiz_slider], outputs=quiz_output)

    with gr.Tab("πŸ“‰ Weakness Analyzer"):
        weak_file = gr.File(label="Upload CSV with Topic & Score columns")
        weak_btn = gr.Button("Analyze")
        weak_out = gr.Textbox(label="Analysis")
        weak_btn.click(fn=analyze_weakness, inputs=weak_file, outputs=weak_out)

    with gr.Tab("πŸ€– Teaching Assistant"):
        gr.ChatInterface(fn=chatbot_response)

    with gr.Tab("🎀 Speech Q Solver"):
        audio_in = gr.Audio(label="Upload Audio", type="filepath")
        audio_btn = gr.Button("Transcribe + Generate Answer")
        audio_out = gr.Textbox(label="Answer")
        audio_btn.click(fn=speech_answer, inputs=audio_in, outputs=audio_out)

    with gr.Tab("πŸ“„ Summarizer"):
        sum_text = gr.Textbox(lines=5, label="Paste Text")
        sum_btn = gr.Button("Summarize")
        sum_out = gr.Textbox(label="Summary")
        sum_btn.click(fn=summarize_text, inputs=sum_text, outputs=sum_out)

    with gr.Tab("πŸ“Š Engagement Predictor"):
        eng_file = gr.File(label="Upload CSV with TimeSpent column")
        eng_btn = gr.Button("Predict")
        eng_out = gr.Textbox()
        eng_btn.click(fn=predict_engagement, inputs=eng_file, outputs=eng_out)

    with gr.Tab("πŸ… Badge Generator"):
        badge_file = gr.File(label="Upload CSV with Score column")
        badge_btn = gr.Button("Get Badge")
        badge_out = gr.Textbox()
        badge_btn.click(fn=generate_badge, inputs=badge_file, outputs=badge_out)

    with gr.Tab("🌍 Translator"):
        trans_in = gr.Textbox(label="Enter Text")
        trans_lang = gr.Textbox(label="Target Language")
        trans_btn = gr.Button("Translate")
        trans_out = gr.Textbox()
        trans_btn.click(fn=translate_text, inputs=[trans_in, trans_lang], outputs=trans_out)

    with gr.Tab("πŸ“‹ Plagiarism Checker"):
        text1 = gr.Textbox(label="Text 1", lines=3)
        text2 = gr.Textbox(label="Text 2", lines=3)
        plag_btn = gr.Button("Check Similarity")
        plag_out = gr.Textbox()
        plag_btn.click(fn=check_plagiarism, inputs=[text1, text2], outputs=plag_out)

# ------------------------------
# Launch
# ------------------------------
demo.launch()