MNGames commited on
Commit
f73ee36
1 Parent(s): eee75cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -7,24 +7,28 @@ tokenizer = BartTokenizer.from_pretrained(model_name)
7
  model = BartForConditionalGeneration.from_pretrained(model_name)
8
 
9
  def detect_questions(email_text):
10
- # Simple heuristic to detect questions
11
- questions = [sentence.strip() + "?" for sentence in email_text.split(".") if "?" in sentence]
12
- return questions
 
 
13
 
14
- def generate_answers(question):
15
- # Generate an answer for the given question using the BART model
16
- inputs = tokenizer(question, return_tensors="pt", max_length=1024, truncation=True)
17
- summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=50, early_stopping=True)
18
- answer = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
19
- return answer
 
 
 
20
 
21
  def process_email(email_text):
 
22
  questions = detect_questions(email_text)
23
- responses = {}
24
 
25
- for question in questions:
26
- response = generate_answers(question)
27
- responses[question] = response
28
 
29
  return responses
30
 
@@ -32,8 +36,8 @@ iface = gr.Interface(
32
  fn=process_email,
33
  inputs="textbox",
34
  outputs="text",
35
- title="Email Question Responder",
36
- description="Input an email, and the AI will detect questions and provide possible answers.",
37
  )
38
 
39
  iface.launch()
 
7
  model = BartForConditionalGeneration.from_pretrained(model_name)
8
 
9
  def detect_questions(email_text):
10
+ # Use the BART model to generate questions from the email text
11
+ inputs = tokenizer("generate questions: " + email_text, return_tensors="pt", max_length=1024, truncation=True)
12
+ questions = model.generate(inputs["input_ids"], num_beams=4, max_length=50, early_stopping=True)
13
+ questions = tokenizer.decode(questions[0], skip_special_tokens=True)
14
+ return questions.split("##")
15
 
16
+ def generate_responses(questions):
17
+ responses = {}
18
+ for question in questions:
19
+ # Generate a response for each question using the BART model
20
+ inputs = tokenizer(question, return_tensors="pt", max_length=1024, truncation=True)
21
+ response = model.generate(inputs["input_ids"], num_beams=4, max_length=200, early_stopping=True)
22
+ response = tokenizer.decode(response[0], skip_special_tokens=True)
23
+ responses[question] = response
24
+ return responses
25
 
26
  def process_email(email_text):
27
+ # Detect questions from the email
28
  questions = detect_questions(email_text)
 
29
 
30
+ # Generate responses to the detected questions
31
+ responses = generate_responses(questions)
 
32
 
33
  return responses
34
 
 
36
  fn=process_email,
37
  inputs="textbox",
38
  outputs="text",
39
+ title="Email Question Detector and Responder",
40
+ description="Input an email, and the AI will detect questions and provide possible responses.",
41
  )
42
 
43
  iface.launch()