MNGames commited on
Commit
05f708c
1 Parent(s): 29b067f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -1,28 +1,44 @@
 
1
  import gradio as gr
2
- from transformers import BartForConditionalGeneration, BartTokenizer
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
- def generate_questions(email_text):
10
- # Preprocess the email text for the BART model
11
- inputs = tokenizer(email_text, return_tensors="pt", max_length=1024, truncation=True)
 
 
12
 
13
- # Generate questions
14
- summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=500, early_stopping=True)
15
- summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
 
 
 
 
16
 
17
- return summary
 
 
 
 
 
 
 
 
 
18
 
19
  # Create a Gradio interface
20
  iface = gr.Interface(
21
- fn=generate_questions,
22
  inputs="textbox",
23
- outputs="textbox",
24
- title="Email Question Generator",
25
- description="Input an email, and the AI will generate the biggest questions that probably need to be answered.",
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