Spaces:
Sleeping
Sleeping
Pranjal12345
commited on
Commit
•
7e9cf5a
1
Parent(s):
0f7a547
Upload 2 files
Browse files- app.py +51 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
import os
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("potsawee/t5-large-generation-squad-QuestionAnswer")
|
8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("potsawee/t5-large-generation-squad-QuestionAnswer")
|
9 |
+
|
10 |
+
def generate_question_answer_pairs(input_text):
|
11 |
+
if input_text is None:
|
12 |
+
return "Please enter a text"
|
13 |
+
|
14 |
+
d = {'Question':[],'Answer':[]}
|
15 |
+
df = pd.DataFrame(data=d)
|
16 |
+
|
17 |
+
sentences = re.split(r'(?<=[.!?])', input_text)
|
18 |
+
question_answer_pairs = []
|
19 |
+
|
20 |
+
for sentence in sentences:
|
21 |
+
input_ids = tokenizer.encode(sentence, return_tensors="pt")
|
22 |
+
outputs = model.generate(input_ids, max_length=100, num_return_sequences=1)
|
23 |
+
question_answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
question_answer_pairs.append(question_answer)
|
25 |
+
|
26 |
+
result = ''
|
27 |
+
|
28 |
+
for question_answer in question_answer_pairs:
|
29 |
+
qa_parts = question_answer.split("?")
|
30 |
+
if len(qa_parts) >= 2:
|
31 |
+
question_part = qa_parts[0] + "?"
|
32 |
+
answer_part = qa_parts[1].strip()
|
33 |
+
new_data = {'Question': [question_part], 'Answer': [answer_part]}
|
34 |
+
df = pd.concat([df, pd.DataFrame(new_data)], ignore_index=True)
|
35 |
+
result += f"Question: {question_part}\nAnswer: {answer_part}\n\n"
|
36 |
+
|
37 |
+
df.to_csv("QAPairs.csv")
|
38 |
+
return result, "QAPairs.csv"
|
39 |
+
|
40 |
+
title = "Question-Answer Pairs Generation"
|
41 |
+
input_text = gr.Textbox(lines=4, label="Text:")
|
42 |
+
output_file = gr.File(label="Download as csv")
|
43 |
+
output_text = gr.Textbox()
|
44 |
+
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=generate_question_answer_pairs,
|
47 |
+
inputs=input_text,
|
48 |
+
outputs=[output_text, output_file],
|
49 |
+
title=title,
|
50 |
+
)
|
51 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|