import json import openai import os from dotenv import load_dotenv load_dotenv() OPENAI_API_KEY=os.getenv("OPENAI_API_KEY") def summarize(all_text: str): MAX_WORDS = 90000 # Keep buffer for OpenAI token limit all_text = " ".join(all_text.split()[:MAX_WORDS]) new_text=" ".join(all_text.split()) print(len(new_text)) model_name = "gpt-4o-mini" client = openai.OpenAI(api_key=OPENAI_API_KEY) response = client.chat.completions.create( model=model_name, response_format={"type": "json_object"}, messages=[ {"role": "system", "content": "You are an AI that summarizes long emails related to the topic'. You must respond in a structured JSON format."}, {"role": "user", "content": f"Summarize this text seeing the context: \n{new_text}. Format the response as follows and give 5 headings and descriptions according to the topic from the context:\n\n{{'response': [{{'heading': 'Title', 'description': 'Summary'}}]}}"} ], max_tokens=2000 ) # Extract structured JSON response final_summary = response.choices[0].message.content.strip() parsed_summary = json.loads(final_summary) # Convert string to JSON return parsed_summary def summarize_week(all_text: str): import openai MAX_WORDS = 90000 # Keep buffer for OpenAI token limit all_text = " ".join(all_text.split()[:MAX_WORDS]) new_text = " ".join(all_text.split()) print(len(new_text)) if len(new_text) == 0: return None model_name = "gpt-4o-mini" client = openai.OpenAI(api_key=OPENAI_API_KEY) response = client.chat.completions.create( model=model_name, messages=[ { "role": "system", "content": ( "You are an AI designed to summarize long emails or documents " "and format them into a **professional, beautiful article** written in **Markdown**. " "Structure the article systematically with a clear title, sections, bullet points, and conclusion." ) }, { "role": "user", "content": ( f"The context is: {new_text}\n\n" "Please summarize the above content into a **well-structured Markdown article** with:\n" "- A clear title\n" "- Proper headings (##)\n" "- Bullet points (where needed)\n" "- Bold text (**text**)\n" "- Italic (*text*)\n" "- Unordered list (- Item 1) \n" "- A conclusion\n" "- Keep it engaging but concise.\n" "- Use Markdown formatting properly.\n" "- Avoid unnecessary repetition.\n" ) } ], max_tokens=2000 ) final_summary = response.choices[0].message.content.strip() return final_summary if __name__=="__main__": text=""" Hello City Council, In advance of the planning meeting tonight, I would like to second my colleague, Dr. Harman Paintal, and his advocacy a focus on youth sports fields with turf and lighting. Please see his email below which I fully support. In addition, I would strongly recommend that the City review its timeline for field allocation and consider moving that process forward 8 weeks. As the President of Stanford Strikers FC, our parents are constantly frustrated that we are not able to tell them when and where their children are practicing until perhaps 2 weeks before our season begins in early August. This process could happen much earlier and all youth sports would benefit from being able to have a schedule set well before signups for their activities each season. As you are aware, scheduling a family these days is a complex task, and our community would greatly appreciate knowing what the fall schedules will look like in early summer so they can plan accordingly. Thank you for your consideration of these items and including the youth sports community in the planning process for the next year. Sincerely, Sara Filipek Menlo Park Mom President of the Board, Stanford Strikers FC Email from Dr. Harman Paintal: Dear esteemed Menlo Park City Council members, I believe that increasing the availability of a flood lit turf field for youth sports should be a city priority. Specifically, field space should be required as part of re-development of the 60 acre SRI campus. I urge you to consider the community amenity aspect of this development. A dedication similar to El Camino Park in Palo Alto across from Stanford Mall or Mayfield Soccer Fields at the corner of Page Mill & El Camino is needed. At Stanford Strikers, a youth community soccer organization, the issues that we constantly face in Menlo Park are: 1. Frequent disruption of youth sports due to weather affecting grass fields. 2. Menlo Park youth driving to other cities due to lack of local field availability. 3. Abbreviated practices for 6 months of the year due to absence of lighted fields. This is in contrast to our neighboring cities up and down the peninsula that support: 1. More regulation size athletic and recreation fields with all-weather turf surfaces. 2. Flood lit fields that allow sports till 9 PM 3. Usage of school fields This is critical to the Menlo Park community as we know the benefits of youth sports: 1. The most important solution to the mental health of our kids is the ability to be outside, to interact and play with other similar aged kids. 2. Prevention of pre-diabetes and other metabolic risk factors 3. Improved school attendance Lack of field space in Menlo Park is directly affecting the health and mental being of our kids. Are there other priorities that rank above the physical, emotional and social health of our youth? I believe that this must be a priority for our city. I think that the SRI development requires a dedication of a regulation size floodlit athletic field. I also think that the city pursue placing turf and lighting at Burgess Park to improve access for all sports. Thank you """ print(summarize(text))