-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmail.py
More file actions
64 lines (52 loc) · 2.85 KB
/
mail.py
File metadata and controls
64 lines (52 loc) · 2.85 KB
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from dotenv import *
import os
import google.generativeai as genai
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from google.generativeai.types.content_types import *
from PIL import Image
def generate_text(text):
load_dotenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel("gemini-pro")
prompt="You are a patient, you will talk legally and professionaly(at least 250 words). I wil; provide you information showing hospital charges for your treatments based on that write a full letter, telling them about how you need compensation back and how you will take it to court otherwise. The letter should not require any additional data. You should be fill in the address and name with provided information. Make sure to only talk about cases where the charge difference is negative, that is where you have been overcharged."
prompt += "Max Verstappen address:1234 main steet, Box Box"
prompt+="hospital name: Box Box Hospital, BOX Box area"
prompt+="Do not leave any blanks like Your name or Date of Surgery or Address or Date of Letter, if you don't have such information don't write about them and write about the stuff that you have, the mail should be ready to be posted and I shouldn't need to add anything to it"
responses = model.generate_content([text, prompt])
return responses.text
def generate_letter_pdf(text, filename):
# Generate text
letter_content = generate_text(text)
# Create PDF
c = canvas.Canvas(filename, pagesize=letter)
# Set font and size
c.setFont("Helvetica", 12)
# Define margins
left_margin = 50
top_margin = 750
line_height = 14 # Adjust as needed
lines = letter_content.split("\n")
max_width = letter[0] - left_margin * 2 # Maximum width for text
for line in lines:
# Split line if exceeds maximum width
while c.stringWidth(line, "Helvetica", 12) > max_width:
# Find the split point
split_index = line.rfind(' ', 0, int(len(line) * max_width / c.stringWidth(line, "Helvetica", 12)))
if split_index == -1: # If no space found within the width, split forcefully
split_index = int(len(line) * max_width / c.stringWidth(line, "Helvetica", 12))
# Split the line
split_line = line[:split_index]
# Draw the line
c.drawString(left_margin, top_margin, split_line.strip())
# Update position
top_margin -= line_height
# Update remaining text
line = line[split_index:].lstrip() # Remove leading spaces after splitting
# Draw the remaining part of the line
c.drawString(left_margin, top_margin, line.strip())
# Update position
top_margin -= line_height
# Save the PDF
c.save()