Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
early_stopping=True
|
| 21 |
-
)
|
| 22 |
-
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
-
return result
|
| 24 |
|
|
|
|
| 25 |
demo = gr.Interface(
|
| 26 |
-
fn=
|
| 27 |
-
inputs=gr.Textbox(lines=
|
| 28 |
-
outputs=gr.Textbox(lines=
|
| 29 |
-
title="
|
| 30 |
-
description="
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from pprint import pprint
|
| 3 |
+
from lmqg import TransformersQG
|
| 4 |
|
| 5 |
+
# Initialize model (CPU-only)
|
| 6 |
+
model = TransformersQG(model='lmqg/t5-base-squad-qg', model_ae='lmqg/t5-base-squad-ae')
|
| 7 |
|
| 8 |
+
# Function to chunk text into smaller parts
|
| 9 |
+
def chunk_text(text, chunk_size=450):
|
| 10 |
+
tokenizer = model.tokenizer
|
| 11 |
+
tokens = tokenizer.encode(text)
|
| 12 |
+
chunks = [tokens[i:i + chunk_size] for i in range(0, len(tokens), chunk_size)]
|
| 13 |
+
return chunks
|
| 14 |
|
| 15 |
+
# Function to process each chunk and generate QA pairs
|
| 16 |
+
def generate_qa_for_chunks(text):
|
| 17 |
+
chunks = chunk_text(text)
|
| 18 |
+
qa_pairs = []
|
| 19 |
+
for chunk in chunks:
|
| 20 |
+
chunk_text = model.tokenizer.decode(chunk, skip_special_tokens=True)
|
| 21 |
+
qa = model.generate_qa(chunk_text)
|
| 22 |
+
qa_pairs.extend(qa)
|
| 23 |
+
return "\n\n".join([f"Q: {item['question']}\nA: {item['answer']}" for item in qa_pairs])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Gradio UI
|
| 26 |
demo = gr.Interface(
|
| 27 |
+
fn=generate_qa_for_chunks,
|
| 28 |
+
inputs=gr.Textbox(lines=20, label="Input Text (SAP Note or Paragraph)"),
|
| 29 |
+
outputs=gr.Textbox(lines=30, label="Generated QA Pairs"),
|
| 30 |
+
title="Question Generator (LMQG - T5)",
|
| 31 |
+
description="Paste text to generate question-answer pairs using lmqg/t5-base-squad-qg"
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|