Commit
·
2360418
1
Parent(s):
b8c967e
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
|
| 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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import re
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/potsawee/t5-large-generation-squad-QuestionAnswer"
|
| 6 |
+
headers = {"Authorization": "Bearer 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"}
|
| 7 |
|
| 8 |
+
def query(payload):
|
| 9 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 10 |
+
return response.json()
|
| 11 |
+
|
| 12 |
def generate_question_answer_pairs(input_text):
|
| 13 |
if input_text is None:
|
| 14 |
return "Please enter a text"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
sentences = re.split(r'(?<=[.!?])', input_text)
|
| 17 |
+
outputs = []
|
| 18 |
|
| 19 |
for sentence in sentences:
|
| 20 |
+
if sentence.strip():
|
| 21 |
+
output = query({
|
| 22 |
+
"inputs": sentence,
|
| 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()
|