Prashanthsrn commited on
Commit
23e35d6
·
verified ·
1 Parent(s): 7ec0c99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -11,36 +11,43 @@ generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
11
  email_history = []
12
 
13
  # Function to generate email, rate it, and suggest changes
14
- def generate_email(subject, previous_draft=None, step_count=1):
15
- # Generate initial email or improved version based on previous draft
16
- if step_count == 1:
17
- prompt = f"Write a formal and professional email on the topic: '{subject}'. Keep it concise, relevant, and polite."
18
- else:
19
- prompt = (f"Here is the current draft of an email about '{subject}':\n\n"
20
- f"{previous_draft}\n\n"
21
- f"Please improve this draft based on the suggestions given: \n\n"
22
- f"Suggestions: {suggestion}\n\n"
23
- f"This is step {step_count}.")
24
-
25
- response = generator(prompt, max_length=300, num_return_sequences=1)
26
- generated_email = response[0]['generated_text']
 
 
 
27
 
28
- # Rate the email and suggest improvements
29
- score_prompt = f"Rate the quality of this email on a scale of 1 to 10: \n\n{generated_email}"
30
- score_response = generator(score_prompt, max_length=30, num_return_sequences=1)
31
- score = score_response[0]['generated_text'].strip()
32
 
33
- suggestion_prompt = f"Suggest specific improvements for this email: \n\n{generated_email}"
34
- suggestion_response = generator(suggestion_prompt, max_length=50, num_return_sequences=1)
35
- suggestion = suggestion_response[0]['generated_text'].strip()
36
 
37
- # Store the email, score, and suggestion
38
- email_history.append({
39
- "step": step_count,
40
- "email": generated_email,
41
- "score": score,
42
- "suggestion": suggestion
43
- })
 
 
 
 
44
 
45
  return generated_email, score, suggestion
46
 
@@ -54,16 +61,7 @@ def view_history():
54
 
55
  # Gradio interface function
56
  def email_agent(subject):
57
- if not email_history:
58
- step_count = 1
59
- previous_draft = None
60
- else:
61
- step_count = email_history[-1]["step"] + 1
62
- previous_draft = email_history[-1]["email"]
63
- # Get the last suggestion for reflection
64
- suggestion = email_history[-1]["suggestion"]
65
-
66
- email, score, suggestion = generate_email(subject, previous_draft, step_count)
67
  history_display = view_history()
68
 
69
  return email, score, suggestion, history_display
 
11
  email_history = []
12
 
13
  # Function to generate email, rate it, and suggest changes
14
+ def generate_email(subject):
15
+ step_count = 1
16
+ previous_draft = None
17
+ score = 0
18
+
19
+ while score < 7:
20
+ # Generate initial email or improved version based on previous draft
21
+ if step_count == 1:
22
+ prompt = f"Write a formal and professional email on the topic: '{subject}'. Keep it concise, relevant, and polite."
23
+ else:
24
+ prompt = (f"Here is the current draft of an email about '{subject}':\n\n"
25
+ f"{previous_draft}\n\n"
26
+ f"This is step {step_count}. Improve this email.")
27
+
28
+ response = generator(prompt, max_length=300, num_return_sequences=1)
29
+ generated_email = response[0]['generated_text']
30
 
31
+ # Rate the email and suggest improvements
32
+ score_prompt = f"Rate the quality of this email on a scale of 1 to 10: \n\n{generated_email}"
33
+ score_response = generator(score_prompt, max_length=30, num_return_sequences=1)
34
+ score = int(score_response[0]['generated_text'].strip())
35
 
36
+ suggestion_prompt = f"Suggest specific improvements for this email: \n\n{generated_email}"
37
+ suggestion_response = generator(suggestion_prompt, max_length=50, num_return_sequences=1)
38
+ suggestion = suggestion_response[0]['generated_text'].strip()
39
 
40
+ # Store the email, score, and suggestion
41
+ email_history.append({
42
+ "step": step_count,
43
+ "email": generated_email,
44
+ "score": score,
45
+ "suggestion": suggestion
46
+ })
47
+
48
+ # Update previous draft and increment step count
49
+ previous_draft = generated_email
50
+ step_count += 1
51
 
52
  return generated_email, score, suggestion
53
 
 
61
 
62
  # Gradio interface function
63
  def email_agent(subject):
64
+ email, score, suggestion = generate_email(subject)
 
 
 
 
 
 
 
 
 
65
  history_display = view_history()
66
 
67
  return email, score, suggestion, history_display