Spaces:
Runtime error
Runtime error
import gradio as gr | |
from gradio.components import Textbox | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration | |
from peft import PeftModel | |
import torch | |
import datasets | |
from sentence_transformers import CrossEncoder | |
# Load cross encoder | |
top_k = 10 | |
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') | |
# Load your fine-tuned model and tokenizer | |
model_name = "google/flan-t5-large" | |
peft_name = "legacy107/flan-t5-large-ia3-covidqa" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large") | |
model = PeftModel.from_pretrained(model, peft_name) | |
peft_name = "legacy107/flan-t5-large-ia3-bioasq-paraphrase" | |
paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
paraphrase_model = PeftModel.from_pretrained(paraphrase_model, peft_name) | |
max_length = 512 | |
max_target_length = 200 | |
# Load your dataset | |
dataset = datasets.load_dataset("minh21/COVID-QA-Chunk-64-testset-biencoder-data-90_10", split="train") | |
dataset = dataset.shuffle() | |
dataset = dataset.select(range(5)) | |
def paraphrase_answer(question, answer): | |
# Combine question and context | |
input_text = f"question: {question}. Paraphrase the answer to make it more natural answer: {answer}" | |
# Tokenize the input text | |
input_ids = tokenizer( | |
input_text, | |
return_tensors="pt", | |
padding="max_length", | |
truncation=True, | |
max_length=max_length, | |
).input_ids | |
# Generate the answer | |
with torch.no_grad(): | |
generated_ids = paraphrase_model.generate(input_ids=input_ids, max_new_tokens=max_target_length) | |
# Decode and return the generated answer | |
paraphrased_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True) | |
return paraphrased_answer | |
def retrieve_context(question, contexts): | |
# cross-encoder | |
hits = [{"corpus_id": i} for i in range(len(contexts))] | |
cross_inp = [[question, contexts[hit["corpus_id"]]] for hit in hits] | |
cross_scores = cross_encoder.predict(cross_inp, show_progress_bar=False) | |
for idx in range(len(cross_scores)): | |
hits[idx]["cross-score"] = cross_scores[idx] | |
hits = sorted(hits, key=lambda x: x["cross-score"], reverse=True) | |
return " ".join( | |
[contexts[hit["corpus_id"]] for hit in hits[0:top_k]] | |
).replace("\n", " ") | |
# Define your function to generate answers | |
def generate_answer(question, context, contexts): | |
if type(contexts) is str: | |
contexts = contexts.split(',') | |
context = retrieve_context(question, contexts) | |
# Combine question and context | |
input_text = f"question: {question} context: {context}" | |
# Tokenize the input text | |
input_ids = tokenizer( | |
input_text, | |
return_tensors="pt", | |
padding="max_length", | |
truncation=True, | |
max_length=max_length, | |
).input_ids | |
# Generate the answer | |
with torch.no_grad(): | |
generated_ids = model.generate(input_ids=input_ids, max_new_tokens=max_target_length) | |
# Decode and return the generated answer | |
generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True) | |
# Paraphrase answer | |
paraphrased_answer = paraphrase_answer(question, generated_answer) | |
return generated_answer, paraphrased_answer | |
# Define a function to list examples from the dataset | |
def list_examples(): | |
examples = [] | |
for example in dataset: | |
context = example["context"] | |
contexts = example["context_chunks"] | |
question = example["question"] | |
examples.append([question, context, contexts]) | |
return examples | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=generate_answer, | |
inputs=[ | |
Textbox(label="Question"), | |
Textbox(label="Context"), | |
Textbox(label="Contexts") | |
], | |
outputs=[ | |
Textbox(label="Generated Answer"), | |
Textbox(label="Natural Answer") | |
], | |
examples=list_examples() | |
) | |
# Launch the Gradio interface | |
iface.launch() |