Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,64 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
|
4 |
-
#
|
5 |
model_name = "facebook/bart-base"
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
2 |
+
from gradio import Interface
|
3 |
|
4 |
+
# Define the model name (change if desired)
|
5 |
model_name = "facebook/bart-base"
|
6 |
+
|
7 |
+
# Load tokenizer and model
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
10 |
+
|
11 |
+
def generate_questions(email):
|
12 |
+
"""Generates questions based on the input email."""
|
13 |
+
# Encode the email with tokenizer
|
14 |
+
inputs = tokenizer(email, return_tensors="pt")
|
15 |
+
|
16 |
+
# Generate questions using model with specific prompt
|
17 |
+
generation = model.generate(
|
18 |
+
input_ids=inputs["input_ids"],
|
19 |
+
max_length=256, # Adjust max length as needed
|
20 |
+
num_beams=5, # Adjust beam search for better quality (slower)
|
21 |
+
early_stopping=True,
|
22 |
+
prompt="What are the important questions or things that need to be addressed in this email:\n",
|
23 |
+
)
|
24 |
+
|
25 |
+
# Decode the generated text
|
26 |
+
return tokenizer.decode(generation[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
def generate_answers(questions):
|
29 |
+
"""Generates possible answers to the input questions."""
|
30 |
+
# Encode each question with tokenizer, separated by newline
|
31 |
+
inputs = tokenizer("\n".join(questions), return_tensors="pt")
|
32 |
+
|
33 |
+
# Generate answers using model with specific prompt
|
34 |
+
generation = model.generate(
|
35 |
+
input_ids=inputs["input_ids"],
|
36 |
+
max_length=512, # Adjust max length as needed
|
37 |
+
num_beams=3, # Adjust beam search for better quality (slower)
|
38 |
+
early_stopping=True,
|
39 |
+
prompt="Here are some possible answers to the questions:\n",
|
40 |
+
)
|
41 |
+
|
42 |
+
# Decode the generated text
|
43 |
+
answers = tokenizer.decode(generation[0], skip_special_tokens=True).split("\n")
|
44 |
+
return zip(questions, answers[1:]) # Skip the first answer (prompt repetition)
|
45 |
+
|
46 |
+
def gradio_app(email):
|
47 |
+
"""Gradio interface function"""
|
48 |
+
questions = generate_questions(email)
|
49 |
+
answers = generate_answers(questions.split("\n"))
|
50 |
+
return questions, [answer for _, answer in answers]
|
51 |
+
|
52 |
+
# Gradio interface definition
|
53 |
+
interface = Interface(
|
54 |
+
fn=gradio_app,
|
55 |
+
inputs="textbox",
|
56 |
+
outputs=["text", "text"],
|
57 |
+
title="AI Email Assistant",
|
58 |
+
description="Enter a long email and get questions and possible answers generated by an AI model.",
|
59 |
+
label="Email",
|
60 |
+
elem_id="email-input"
|
61 |
)
|
62 |
|
63 |
+
# Launch the Gradio interface
|
64 |
+
interface.launch()
|