Spaces:
Sleeping
Sleeping
Commit
·
219dfe0
1
Parent(s):
7e9cf5a
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,50 +2,37 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
import os
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
| 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
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 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=
|
| 49 |
title=title,
|
| 50 |
)
|
| 51 |
interface.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/potsawee/t5-large-generation-squad-QuestionAnswer"
|
| 7 |
+
headers = {"Authorization": "Bearer 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"}
|
| 8 |
|
| 9 |
+
def query(payload):
|
| 10 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 11 |
+
return response.json()
|
| 12 |
+
|
| 13 |
def generate_question_answer_pairs(input_text):
|
| 14 |
if input_text is None:
|
| 15 |
return "Please enter a text"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
sentences = re.split(r'(?<=[.!?])', input_text)
|
| 18 |
+
outputs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
for i in range(len(sentences)):
|
| 21 |
+
output = query({
|
| 22 |
+
"inputs": sentences[i],
|
| 23 |
+
})
|
| 24 |
+
outputs.append(output)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
return outputs
|
|
|
|
| 27 |
|
| 28 |
title = "Question-Answer Pairs Generation"
|
| 29 |
input_text = gr.Textbox(lines=4, label="Text:")
|
|
|
|
| 30 |
output_text = gr.Textbox()
|
| 31 |
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=generate_question_answer_pairs,
|
| 34 |
inputs=input_text,
|
| 35 |
+
outputs=output_text,
|
| 36 |
title=title,
|
| 37 |
)
|
| 38 |
interface.launch()
|