-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenai_summarizer.py
More file actions
69 lines (50 loc) · 1.93 KB
/
genai_summarizer.py
File metadata and controls
69 lines (50 loc) · 1.93 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
65
66
67
68
69
import google.generativeai as genai
import os
import json
# Load the config file
with open('config.json', "r") as file:
config = json.load(file)
# Configure Google API key
genai.configure(api_key=config.get("google_api_key"))
def summarize_code(file_path):
"""
Reads a .py file and generates a summary using Google's Generative AI API.
Args:
file_path (str): Path to the Python file to summarize.
Returns:
str: The summary of the Python code or an error message.
"""
try:
if not os.path.exists(file_path):
return "Error: File not found. Please provide a valid file path."
with open(file_path, "r", encoding="utf-8") as file:
code_content = file.read()
model = genai.GenerativeModel("gemini-1.5-flash")
prompt = f"Summarize the following Python code:\n\n{code_content}"
response = model.generate_content(prompt)
return response.text.strip() if response else "Error: No response from AI."
except Exception as e:
return f"Error during summarization: {str(e)}"
def generate_email_content(receiver_name, summary, sender_name):
"""
Generates an email message using Google's Generative AI.
Args:
receiver_name (str): Name of the email recipient.
summary (str): The summarized Python code.
sender_name (str): Name of the sender.
Returns:
str: A formatted email message.
"""
try:
model = genai.GenerativeModel("gemini-1.5-flash")
prompt = f"""Write a friendly and professional email.
Hey {receiver_name},
Today's progress is:
{summary}
Thanks & Regards,
{sender_name}
"""
response = model.generate_content(prompt)
return response.text.strip() if response else "Error: No response from AI."
except Exception as e:
return f"Error generating email content: {str(e)}"