import gradio as gr from gradio.components import Textbox from transformers import AutoTokenizer, AutoModelForSeq2SeqLM import torch import datasets # Load your fine-tuned model and tokenizer model_name = "legacy107/flan-t5-large-bottleneck-adapter-cpgQA-unique" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) model.set_active_adapters("question_answering") max_length = 512 max_target_length = 128 # Load your dataset dataset = datasets.load_dataset("minh21/cpgQA-v1.0-unique-context-test-10-percent", split="test[:10]") # Define your function to generate answers def generate_answer(question, context): # 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, max_new_tokens=max_target_length) # Decode and return the generated answer generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True) return generated_answer # Define a function to list examples from the dataset def list_examples(): examples = [] for example in dataset: context = example["context"] question = example["question"] examples.append([question, context]) return examples # Create a Gradio interface iface = gr.Interface( fn=generate_answer, inputs=[ Textbox(label="Question"), Textbox(label="Context") ], outputs=Textbox(label="Generated Answer"), examples=list_examples() ) # Launch the Gradio interface iface.launch()