Spaces:
Running
Running
# app.py | |
import gradio as gr | |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM | |
import torch | |
# ====== Load AI models ====== | |
# Summarization model (BART) | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
# ====== Question / Flashcard model (T5 small) ====== | |
tokenizer = AutoTokenizer.from_pretrained("valhalla/t5-small-qg-hl", use_fast=False) | |
model = AutoModelForSeq2SeqLM.from_pretrained("valhalla/t5-small-qg-hl") | |
# ====== Helper function for manual generation ====== | |
def generate_questions(text, max_length=128): | |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) | |
outputs = model.generate(**inputs, max_length=max_length) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# ====== Summarizer Function ====== | |
def summarize_text(text, mode): | |
if not text.strip(): | |
return "⚠️ Please provide some notes!" | |
if mode == "short": | |
max_len, min_len = 60, 20 | |
elif mode == "revision": | |
max_len, min_len = 120, 40 | |
else: # long | |
max_len, min_len = 250, 80 | |
summary = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False) | |
return summary[0]['summary_text'] | |
# ====== Quiz Generator ====== | |
def generate_quiz(text): | |
if not text.strip(): | |
return "⚠️ Please provide some notes!" | |
return generate_questions(f"generate questions: {text}") | |
# ====== Flashcards Generator ====== | |
def generate_flashcards(text): | |
if not text.strip(): | |
return "⚠️ Please provide some notes!" | |
return generate_questions(f"generate flashcards: {text}") | |
# ====== Gradio Interface ====== | |
with gr.Blocks() as demo: | |
gr.Markdown("## 📝 AI Notes Summarizer, Quiz & Flashcards") | |
with gr.Tab("Summarizer"): | |
input_text = gr.Textbox(lines=10, placeholder="Paste your notes here...") | |
mode = gr.Radio(["short", "revision", "long"], value="revision", label="Summary Type") | |
summary_output = gr.Textbox(label="Summary") | |
gr.Button("Summarize").click(fn=summarize_text, inputs=[input_text, mode], outputs=[summary_output]) | |
with gr.Tab("Quiz Generator"): | |
quiz_input = gr.Textbox(lines=10, placeholder="Paste your notes or summary here...") | |
quiz_output = gr.Textbox(label="Quiz Questions") | |
gr.Button("Generate Quiz").click(fn=generate_quiz, inputs=[quiz_input], outputs=[quiz_output]) | |
with gr.Tab("Flashcards Generator"): | |
flash_input = gr.Textbox(lines=10, placeholder="Paste your notes or summary here...") | |
flash_output = gr.Textbox(label="Flashcards (Q&A)") | |
gr.Button("Generate Flashcards").click(fn=generate_flashcards, inputs=[flash_input], outputs=[flash_output]) | |
demo.launch() | |