Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,44 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
# Load the BART model and tokenizer
|
| 5 |
model_name = "facebook/bart-large-cnn"
|
| 6 |
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 7 |
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Create a Gradio interface
|
| 20 |
iface = gr.Interface(
|
| 21 |
-
fn=
|
| 22 |
inputs="textbox",
|
| 23 |
-
outputs="
|
| 24 |
-
title="Email Question
|
| 25 |
-
description="Input an email, and the AI will
|
| 26 |
)
|
| 27 |
|
| 28 |
# Launch the interface
|
|
|
|
| 1 |
+
import re
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline, BartTokenizer, BartForConditionalGeneration
|
| 4 |
|
| 5 |
+
# Load the BART model and tokenizer for text generation (answer suggestions)
|
| 6 |
model_name = "facebook/bart-large-cnn"
|
| 7 |
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 8 |
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Question detection function
|
| 11 |
+
def detect_questions(email_text):
|
| 12 |
+
# Use regex to find questions in the email
|
| 13 |
+
questions = re.findall(r'([^\.\!\?]*\?)', email_text)
|
| 14 |
+
return questions
|
| 15 |
|
| 16 |
+
# Generate answers using the BART model
|
| 17 |
+
def generate_answers(question):
|
| 18 |
+
# Use the BART model to generate a response
|
| 19 |
+
inputs = tokenizer(question, return_tensors="pt", max_length=1024, truncation=True)
|
| 20 |
+
summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=50, early_stopping=True)
|
| 21 |
+
answer = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 22 |
+
return answer
|
| 23 |
|
| 24 |
+
# Main function to handle the email input
|
| 25 |
+
def process_email(email_text):
|
| 26 |
+
questions = detect_questions(email_text)
|
| 27 |
+
responses = {}
|
| 28 |
+
|
| 29 |
+
for question in questions:
|
| 30 |
+
response = generate_answers(question)
|
| 31 |
+
responses[question] = response
|
| 32 |
+
|
| 33 |
+
return responses
|
| 34 |
|
| 35 |
# Create a Gradio interface
|
| 36 |
iface = gr.Interface(
|
| 37 |
+
fn=process_email,
|
| 38 |
inputs="textbox",
|
| 39 |
+
outputs="json",
|
| 40 |
+
title="Email Question Detector and Responder",
|
| 41 |
+
description="Input an email, and the AI will detect questions and provide suggested responses.",
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the interface
|