import gradio as gr from transformers import pipeline, BartTokenizer, BartForConditionalGeneration # Load the BART model and tokenizer for text generation model_name = "facebook/bart-base" tokenizer = BartTokenizer.from_pretrained(model_name) model = BartForConditionalGeneration.from_pretrained(model_name) def detect_questions(email_text): # Use the BART model to generate questions from the email text inputs = tokenizer("generate questions: " + email_text, return_tensors="pt", max_length=1024, truncation=True) questions = model.generate(inputs["input_ids"], num_beams=4, max_length=50, early_stopping=True) questions = tokenizer.decode(questions[0], skip_special_tokens=True) return questions.split("##") def generate_responses(questions): responses = {} for question in questions: # Generate a response for each question using the BART model inputs = tokenizer(question, return_tensors="pt", max_length=1024, truncation=True) response = model.generate(inputs["input_ids"], num_beams=4, max_length=200, early_stopping=True) response = tokenizer.decode(response[0], skip_special_tokens=True) responses[question] = response return responses def process_email(email_text): # Detect questions from the email questions = detect_questions(email_text) # Generate responses to the detected questions responses = generate_responses(questions) return responses iface = gr.Interface( fn=process_email, inputs="textbox", outputs="text", title="Email Question Detector and Responder", description="Input an email, and the AI will detect questions and provide possible responses.", ) iface.launch()