Spaces:
Sleeping
Sleeping
Add CORS middleware to FastAPI app and update summarize endpoint to accept multiple input fields (urls, codes, notes). Enhance graph logic to process new input structure and return detailed summary response.
948e715
from typing import TypedDict, Optional | |
import os | |
from dotenv import load_dotenv | |
from google import genai | |
from langgraph.graph import StateGraph | |
from pydantic import Field , BaseModel | |
load_dotenv() | |
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch, UrlContext | |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") | |
class SummaryResponse(BaseModel): | |
title: Optional[str] = Field(default=None , description="The title of the content") | |
category: Optional[str] = Field(default=None , description="The category of the content") | |
tags: Optional[list[str]] = Field(default=[] , description="The tags of the content") | |
references: Optional[list[str]] = Field(default=[] , description="The references of the content") | |
summary: Optional[str] = Field(default="" , description="The summary of the content") | |
tools = [] | |
tools.append(Tool(url_context=UrlContext)) | |
tools.append(Tool(google_search=GoogleSearch)) | |
class State(TypedDict): | |
urls: Optional[list[str]] = Field(default=[]) | |
codes: Optional[str] = Field(default="") | |
notes: Optional[str] = Field(default="") | |
summary_content: Optional[str] = Field(default="" , description="The summary of all urls, codes, and notes") | |
title: Optional[str] = Field(default="" , description="The title of the content") | |
category: Optional[str] = Field(default="" , description="The category of the content") | |
tags: Optional[list[str]] = Field(default=[] , description="The tags of the content") | |
references: Optional[list[str]] = Field(default=[] , description="The references of the content") | |
summary_response: Optional[SummaryResponse] = Field(default=None , description="The summary of the content") | |
def summarize_user_input(state: State) -> State: | |
client = genai.Client(api_key=GOOGLE_API_KEY) | |
response = client.models.generate_content( | |
model="gemini-2.5-flash-preview-05-20", | |
config=GenerateContentConfig( | |
tools=tools, | |
system_instruction="You are a helpful assistant that summarizes from urls, codes, and notes", | |
), | |
contents=f""" | |
Summarize the following urls, codes, and notes: | |
Urls: { | |
state["urls"] | |
} | |
Codes: {state["codes"]} | |
Notes: {state["notes"]} | |
And Give the complete summary to as blog to post on medium.com | |
Give the title of the blog. | |
Give the category of the blog. | |
Give the tags of the blog. | |
Search and always give five references from the internet to support the summary. | |
""" | |
) | |
summary_response = response.text | |
state["summary_response"] = summary_response | |
return state | |
def get_summary_response(state: State) -> State: | |
client = genai.Client(api_key=GOOGLE_API_KEY) | |
response = client.models.generate_content( | |
model="gemini-2.5-flash-preview-05-20", | |
contents="Structured the summary of the content . The summary is: " + state["summary_response"] + " and give the title, category, tags, and references", | |
config={ | |
"response_mime_type": "application/json", | |
"response_schema": SummaryResponse, | |
}, | |
) | |
summary : SummaryResponse = response.parsed | |
state["title"] = summary.title | |
state["category"] = summary.category | |
state["tags"] = summary.tags | |
state["references"] = summary.references | |
state["summary_content"] = summary.summary | |
return state | |
builder = StateGraph(State) | |
builder.add_node("summarize", summarize_user_input) | |
builder.add_node("get_summary_response", get_summary_response) | |
builder.add_edge("__start__", "summarize") | |
builder.add_edge("summarize", "get_summary_response") | |
builder.add_edge("get_summary_response", "__end__") | |
graph = builder.compile() | |
graph.name = "summarize_agent" | |