Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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
|