|
import re |
|
import gradio as gr |
|
from transformers import pipeline, BartTokenizer, BartForConditionalGeneration |
|
|
|
|
|
model_name = "facebook/bart-large-cnn" |
|
tokenizer = BartTokenizer.from_pretrained(model_name) |
|
model = BartForConditionalGeneration.from_pretrained(model_name) |
|
|
|
|
|
def detect_questions(email_text): |
|
|
|
questions = re.findall(r'([^\.\!\?]*\?)', email_text) |
|
return questions |
|
|
|
|
|
def generate_answers(question): |
|
|
|
inputs = tokenizer(question, return_tensors="pt", max_length=1024, truncation=True) |
|
summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=50, early_stopping=True) |
|
answer = tokenizer.decode(summary_ids[0], skip_special_tokens=True) |
|
return answer |
|
|
|
|
|
def process_email(email_text): |
|
questions = detect_questions(email_text) |
|
responses = {} |
|
|
|
for question in questions: |
|
response = generate_answers(question) |
|
responses[question] = response |
|
|
|
return responses |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_email, |
|
inputs="textbox", |
|
outputs="json", |
|
title="Email Question Detector and Responder", |
|
description="Input an email, and the AI will detect questions and provide suggested responses.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|