import gradio as gr from transformers import pipeline from PIL import Image import torch from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize from transformers import CLIPProcessor, CLIPModel, BartTokenizer, BartForConditionalGeneration, GPT2LMHeadModel, GPT2Tokenizer, T5ForConditionalGeneration, T5Tokenizer # Load the RoBERTa model roberta_model = pipeline("question-answering", model="deepset/roberta-base-squad2") # Define the interface function for RoBERTa def answer_question_roberta(context, question): result = roberta_model(question=question, context=context) return result["answer"] # Create the Gradio interface for RoBERTa roberta_interface = gr.Interface( fn=answer_question_roberta, inputs=["text", "text"], outputs="text", title="Question Answering", description="Ask a question about the given context. (roBERTa model)", ) # Load the CLIP model and processor clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16") clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16") # Define the interface function for CLIP def classify_image(image: Image.Image, labels: str): # Prepare the image transform = Compose([ Resize([224, 224]), CenterCrop(224), ToTensor(), Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)), ]) image = transform(image) # Prepare the labels labels = labels.split(',') # Encode the image and labels inputs = clip_processor(text=labels, images=image, return_tensors="pt", padding=True) # Get the model's output outputs = clip_model(**inputs) logits_per_image = outputs.logits_per_image # Get the predicted label predicted_label = labels[torch.argmax(logits_per_image).item()] return predicted_label # Create the Gradio interface for CLIP patch16_interface = gr.Interface( fn=classify_image, # The function for image classification inputs=["image", "text"], # Input components outputs="text", # Output component title="Image Classification", # Title of the interface description="Upload an image and enter a list of labels (comma-separated). (with CLIP)", ) # Load the BART model and tokenizer bart_model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn") bart_tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn") # Define the interface function for BART def summarize_text(input_text: str): # Encode the text inputs = bart_tokenizer([input_text], max_length=1024, return_tensors='pt') # Get the model's output summary_ids = bart_model.generate(inputs['input_ids'], num_beams=4, max_length=150, early_stopping=True) # Decode the output summary = [bart_tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids] return summary[0] # Create the Gradio interface for BART bart_large_cnn_interface = gr.Interface( fn=summarize_text, inputs="text", outputs="text", title="Text Summarization", description="Enter a long piece of text. The model will generate a summary. (with BART)", ) # Load the GPT-2 model and tokenizer gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2") gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Define the interface function for GPT-2 def generate_text(prompt: str): # Encode the prompt inputs = gpt2_tokenizer.encode(prompt, return_tensors="pt") # Generate a sequence of tokens outputs = gpt2_model.generate(inputs, max_length=150, temperature=0.7, num_return_sequences=1) # Decode the tokens into a string text = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True) return text # Create the Gradio interface for GPT-2 gpt2_interface = gr.Interface( fn=generate_text, inputs="text", outputs="text", title="Text Generation", description="Enter a prompt and the model will generate a continuation of the text. (with GPT-2)", ) # Load the T5 model and tokenizer t5_model = T5ForConditionalGeneration.from_pretrained("vennify/t5-base-grammar-correction") t5_tokenizer = T5Tokenizer.from_pretrained("vennify/t5-base-grammar-correction") # Define the interface function for T5 def correct_grammar(input_text: str): # Encode the text inputs = t5_tokenizer.encode("correct: " + input_text, return_tensors="pt") # Generate a sequence of tokens outputs = t5_model.generate(inputs, max_length=512, num_beams=4, early_stopping=True) # Decode the tokens into a string corrected_text = t5_tokenizer.decode(outputs[0], skip_special_tokens=True) return corrected_text # Create the Gradio interface for T5 correct_grammar_interface = gr.Interface( fn=correct_grammar, inputs="text", outputs="text", title="Grammar Correction", description="Enter a sentence and the model will correct the grammar. (with T5)", ) # Define the multi-model interface function that summarizes text and answers a question def summarize_and_answer(context, question): # Summarize the text using BART inputs = bart_tokenizer([context], max_length=1024, return_tensors='pt') summary_ids = bart_model.generate(inputs['input_ids'], num_beams=4, max_length=150, early_stopping=True) summary = bart_tokenizer.decode(summary_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False) # Answer the question using RoBERTa answer = roberta_model(question=question, context=summary)["answer"] return summary, answer # Create the Gradio interface for the multi-model task multi_model_interface = gr.Interface( fn=summarize_and_answer, inputs=["text", "text"], outputs=["text", "text"], title="Summarize and Question Answering", description="Provide a text and a question. The interface will first summarize the text and then answer the question based on the summary.", ) # Combine interfaces into a tabbed interface demo = gr.TabbedInterface( [roberta_interface, patch16_interface, bart_large_cnn_interface, gpt2_interface, correct_grammar_interface, multi_model_interface], ["Single-Model: Question Answering", "Single-Model: Image Classification", "Single-Model: Text Summarization", "Single-Model: Text Generation", "Single-Model: Grammar Correction", "Multi-Model: Summarization and Question Answering"] ) # Launch the tabbed interface if __name__ == "__main__": demo.launch()