# app.py import os import uuid from datetime import datetime from typing import Annotated, TypedDict, List, Any, Optional, Dict from dotenv import load_dotenv from pydantic import BaseModel, Field import fitz # PyMuPDF import gradio as gr from langchain_openai import ChatOpenAI from langchain.schema import SystemMessage, HumanMessage, AIMessage from langchain.memory import ConversationSummaryMemory from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from langgraph.checkpoint.memory import MemorySaver # --------------------------- # Load environment variables # --------------------------- load_dotenv() # --------------------------- # State Definition # --------------------------- class State(TypedDict): messages: Annotated[list[Any], add_messages] job_desc: str # keep the original job description user_tone: str # user-specified tone selected_tone: str # optional tone selection phases: list # <-- replace `job_data` with the phases output proposal_text: str # generated proposal text evaluation_scores: dict # scoring for AI detection etc. user_portfolio_text: str # user’s portfolio content user_feedback: dict # feedback if any session_summary: str # summary of the chat/session success_status: str # success/failure flag instructions: str # instruction for proposal edits count_loop: int # for retry or loop logic # --------------------------- # Phase Models # --------------------------- class PhaseStep(BaseModel): step_number: int = Field( description="The sequential order of this step within the phase, starting from 1" ) title: str = Field( description="A short, descriptive title summarizing the step's action, e.g., 'Analyze Job Description'" ) description: str = Field( description="A detailed explanation of what needs to be done in this step" ) tools_required: List[str] = Field( default=[], description="A list of tools, software, or platforms required to complete this step" ) output_example: Optional[str] = Field( default=None, description="An example of the expected output or deliverable for this step, if applicable" ) notes: Optional[str] = Field( default=None, description="Additional notes, tips, or context to ensure the step is carried out correctly" ) class Phase(BaseModel): phase_name: str = Field( description="The name of the phase, grouping related steps, e.g., 'Preparation', 'Execution', 'Delivery'" ) tasks: List[PhaseStep] = Field( description="A list of PhaseStep objects detailing each actionable task in this phase" ) class Phases(BaseModel): phases: List[Phase] = Field( description="A complete list of all phases for the job, each containing structured step-by-step tasks" ) # --------------------------- # Load environment variables and initialize LLM # --------------------------- load_dotenv() # Get OpenAI API key api_key = os.getenv("OPENAI_API_KEY") if not api_key: raise ValueError("OPENAI_API_KEY not found in environment variables") print("✅ API Key loaded successfully.") # Initialize LLM agent agent = ChatOpenAI( model="gpt-4o-mini", api_key=api_key, temperature=0.0, streaming=True ) # Structured output wrapper for Phases node1_agent_llm_with_output = agent.with_structured_output(Phases) # --------------------------- # Phase Maker Function # --------------------------- def phase_maker(state: State) -> Dict[str, Any]: """Generates structured phases from job description and portfolio text.""" print("---- Entered PhaseMaker ----") job_description = state["job_desc"] portfolio_text = state.get("user_portfolio_text", "") print(f"[PhaseMaker] Received job_desc: {job_description[:100]}...") print(f"[PhaseMaker] Received portfolio_text: {portfolio_text[:100]}...") node_1_user_prompt = f""" You are a step-by-step implementation assistant. Your task is to convert the following job description and past work history into a fully structured plan that can be used to generate a proposal. Output MUST follow the Pydantic model `Phases`: - Phases → Phase → PhaseStep - Every step must have: step_number, title, description, tools_required, output_example, notes - Every variable must be fully populated with realistic, specific information. Do NOT leave placeholders or generic statements. - Include all relevant tools, software, and platforms needed for each step. - Include expected deliverables/output in each step if applicable. - Provide actionable instructions that someone could follow to complete each step. - Organize steps logically into phases like Preparation, Execution, Delivery. Requirements: - Ensure clarity, specificity, and actionable content. - Avoid generic or filler text. - Reflect relevant skills and tools used in your past projects if applicable. - Strictly make these 3 phases only: Preparation, Execution, Delivery. Example expected output: {{ "phases": [ {{ "phase_name": "Preparation", "tasks": [ {{ "step_number": 1, "title": "Analyze Job Description", "description": "Carefully read the job posting to identify core tasks and client expectations...", "tools_required": ["Notion", "Google Docs"], "output_example": "List of required skills and deliverables", "notes": "Focus on mandatory skills and desired experience" }} ] }} ] }} Generate the full Phases JSON now. Job Description: {job_description} Past Work History: {portfolio_text} """ if not state.get("phases"): print("[PhaseMaker] No existing phases found → generating new ones...") messages = [HumanMessage(content=node_1_user_prompt)] response = node1_agent_llm_with_output.invoke(messages) print("[PhaseMaker] Generated Phases response successfully ✅") return {"phases": response} print("[PhaseMaker] Existing phases found → skipping generation.") return {"phases": state.get("phases")} # --------------------------- # Node 2 Prompt Template # --------------------------- node_2_prompt = """ You are a professional proposal writing assistant. Your task is to generate a personalized proposal using a predefined template. Follow these rules strictly: 1. Use the `phases` JSON provided as structured output from the previous step. 2. Replace all placeholders in the template with the actual, specific content from `phases` and the job description. 3. Do NOT invent new content beyond what is in `phases`, `job_description`, or `user_portfolio_text`. 4. Maintain a natural, human-like writing style to minimize AI detection. 5. Output the proposal as a single coherent text, ready to send to the client. 6. If `instructions` are provided by the user, apply them carefully without regenerating phases. When filling the placeholders, extract details directly from phases. """ # --------------------------- # Proposal Output & LLM Wrapper # --------------------------- from pydantic import BaseModel, Field from typing import Dict, Any, List from langchain_openai import ChatOpenAI from langchain.schema import HumanMessage, AIMessage, SystemMessage class ProposalOutput(BaseModel): """The full, ready-to-send proposal generated for the client.""" proposal: str = Field( description="The full, ready-to-send proposal generated for the client, written in natural human-like style." ) # Wrap LLM output with structured ProposalOutput agent_llm_with_output = agent.with_structured_output(ProposalOutput) # --------------------------- # Template Selection Function # --------------------------- def get_template_by_tone(tone: str) -> str: """Return proposal template text based on user-selected tone.""" print(f"[Template] Getting template for tone: {tone}") templates = { "Professional & Confident": "[HOOK] Hi {client_name}, I had a look at your project on {final_phase_main_output} ...", "Friendly & Conversational": "[HOOK] Hi {client_name}, I just checked out your project on {project_type} ...", "Creative & Narrative-Driven": "[HOOK] Hi {client_name}, I came across your project on {project_type} ...", "Analytical & Problem-Solver": "[HOOK] from {preparation_phase_decription_short} to the '{final_phase_delieverable}' ...", "Persuasive & Client-Focused": "[HOOK] Picture this: your {project_type} not only looks amazing, but actually works hard ..." } return templates.get(tone, "Tone not found. Please choose a valid tone.") # --------------------------- # Worker Node Function # --------------------------- def worker(state: State) -> Dict[str, Any]: """Generate a client-ready proposal based on job description, phases, and user portfolio.""" print("---- Entered Worker ----") job_message = state["job_desc"] tone = state.get("user_tone", "") portfolio_text = state.get("user_portfolio_text", "") instructions = state.get("instructions", "") template_text = get_template_by_tone(tone) phases = state.get("phases", []) print(f"[Worker] Job description received: {job_message[:100]}...") print(f"[Worker] Tone: {tone}") print(f"[Worker] Instructions: {instructions[:100]}...") print(f"[Worker] Portfolio text: {portfolio_text[:100]}...") print(f"[Worker] Phases present? {'Yes' if phases else 'No'}") node_2_user_prompt = f""" - job_description: {job_message} - phases: structured step-by-step implementation {phases} - user_portfolio_text: {portfolio_text} - instructions: optional user edits for the proposal {instructions} Template: {template_text} """ # Handle feedback if state.get("feedback_on_work"): print("[Worker] Feedback found → appending to system message.") feedback_text = f""" Previous submission was rejected. Here is the feedback you received: {state['feedback_on_work']} Use this feedback to revise and improve the proposal to meet the success criteria. """ else: feedback_text = "" print("[Worker] No feedback found → proceeding normally.") # Build messages found_system_message = False messages = state.get("messages", []) for message in messages: if isinstance(message, SystemMessage): message.content = node_2_user_prompt + feedback_text found_system_message = True if not found_system_message: print("[Worker] No existing SystemMessage → creating a new one.") messages = [SystemMessage(content=node_2_prompt + feedback_text)] + messages else: print("[Worker] Existing SystemMessage found → updated with new prompt.") msg_list = [HumanMessage(content=node_2_user_prompt)] + messages print(f"[Worker] Built msg_list with {len(msg_list)} messages") # Call LLM response = agent_llm_with_output.invoke(msg_list) print("[Worker] Received response from agent LLM ✅") new_state = { "proposal_text": response.proposal, "messages": [AIMessage(content=f"proposal written {response.proposal[:100]}...")] } print("[Worker] Returning new state with proposal_text and updated messages.") return new_state # --------------------------- # Utility Function: Format Conversation # --------------------------- def format_conversation(messages: List[Any]) -> str: """Format the conversation history for display or logging.""" conversation = "Conversation history:\n\n" for message in messages: if isinstance(message, HumanMessage): conversation += f"User: {message.content}\n" elif isinstance(message, AIMessage): conversation += f"Assistant: {message.content}\n" return conversation # --------------------------- # Evaluator Output & LLM Wrapper # --------------------------- from pydantic import BaseModel, Field from typing import Dict, Any from langchain.schema import SystemMessage, HumanMessage from langgraph.graph import StateGraph class EvaluatorOutput(BaseModel): """Structured evaluation output for proposal quality.""" user_feedback: str = Field( description="Detailed feedback on the assistant's proposal, including strengths and areas to improve" ) evaluation_scores: str = Field( description="Scoring or boolean evaluation for each success criterion" ) success_status: str = Field( description="Overall status of evaluation: 'passed', 'failed', or 'needs_input'" ) # System prompt for the evaluator evaluator_system_prompt = """You are an evaluator for a professional proposal-writing AI assistant. You will be given: 1. The job description (client’s posting). 2. The template and tone that were used to generate the proposal. 3. The structured phases the assistant followed. 4. The assistant’s final proposal. Your job is to check whether the proposal aligns with both: - The client’s job description. - The intended template/phases. Check the proposal for: - Relevance — Does it clearly address the specific job description and client needs, not just generic text? - Profile Use — Does it correctly use the freelancer’s profile and portfolio as its own? - Tone — Does the proposal’s tone match the selected template? - Structure — Is the structure aligned with the phases and template (clear, professional flow)? - Portfolio — Are relevant portfolio links included if provided? - Authenticity — Does it avoid robotic or filler text, and sound human? Respond with: 1. A list of feedback points for improvement. 2. A score (0–10) for each: relevance, tone, structure. 3. Overall status: passed, failed, or needs_input. If status is failed, explicitly explain what must be changed in the proposal to pass. """ # --------------------------- # Evaluator Function # --------------------------- def evaluator(state: State) -> State: """Evaluate the generated proposal for quality, alignment with job description, and template adherence.""" print("---- Entered Evaluator ----") last_response = state.get("proposal_text", "") user_message = f"""Conversation history: {format_conversation(state.get('messages', []))} # Job description: {state.get('job_desc', '')} # Template used: {state.get('selected_template', '')} # Structured phases followed: {state.get('phases', '')} Last proposal: {last_response} """ if state.get("user_feedback"): user_message += f"\nPrevious feedback: {state['user_feedback']}\n" print("[Evaluator] Found user feedback → appended to prompt.") else: print("[Evaluator] No previous user feedback.") # Build evaluator messages evaluator_messages = [ SystemMessage(content=evaluator_system_prompt), HumanMessage(content=user_message) ] print(f"[Evaluator] Built evaluator_messages with {len(evaluator_messages)} messages") # LLM call with structured output evaluator_llm_with_output = agent.with_structured_output(EvaluatorOutput) eval_result = evaluator_llm_with_output.invoke(evaluator_messages) print("[Evaluator] Got evaluation result from LLM ✅") print(f"[Evaluator] success_status: {eval_result.success_status}") print(f"[Evaluator] evaluation_scores: {eval_result.evaluation_scores}") print(f"[Evaluator] user_feedback: {eval_result.user_feedback[:100]}...") # Increment loop counter state["count_loop"] = state.get("count_loop", 0) + 1 print(f"[Evaluator] Evaluation loop count → {state['count_loop']}") # Stop after 2 attempts (3rd means needs input) success_status = eval_result.success_status if state["count_loop"] >= 2: print(f"[Evaluator] Maximum limit reached ({state['count_loop']} attempts). Forcing 'needs_input'") success_status = "needs_input" new_state = { "user_feedback": eval_result.user_feedback, "evaluation_scores": eval_result.evaluation_scores, "success_status": success_status, "proposal_text": last_response, "count_loop": state["count_loop"] } print("[Evaluator] Returning new state with evaluation results.") return new_state # --------------------------- # Route Based on Evaluation # --------------------------- def route_based_on_evaluation(state: dict) -> str: """Determine the next step in the workflow based on evaluation status.""" if state.get("success_status") in ["passed", "needs_input"]: return "END" return "worker" # --------------------------- # State Graph Initialization # --------------------------- graph_builder = StateGraph(State) # --------------------------- # Graph Nodes & Flow Setup # --------------------------- from langgraph.checkpoint.memory import MemorySaver from IPython.display import Image, display # Add nodes graph_builder.add_node("phase_maker", phase_maker) graph_builder.add_node("worker", worker) graph_builder.add_node("evaluator", evaluator) # Add edges graph_builder.add_edge("phase_maker", "worker") # phases go into worker graph_builder.add_edge("worker", "evaluator") # proposal goes to evaluator graph_builder.add_conditional_edges( "evaluator", route_based_on_evaluation, {"worker": "worker", "END": END} # loop back or end ) # Entry point graph_builder.add_edge(START, "phase_maker") # Compile the graph with memory memory = MemorySaver() compiled_graph = graph_builder.compile(checkpointer=memory) # # Optional: Display graph as PNG in Jupyter # display(Image(compiled_graph.get_graph().draw_mermaid_png())) # --------------------------- # Thread & Reset Utilities # --------------------------- import uuid def make_thread_id() -> str: """Generate a unique thread ID for new conversation sessions.""" return str(uuid.uuid4()) def reset(thread: str): """Reset conversation memory for a given thread and generate a new thread ID.""" print(f"[Reset] Current thread id: {thread}") try: memory.delete(thread) # Clear old conversation memory except Exception as e: print(f"[Warning] Could not delete memory for thread {thread}: {e}") new_thread = make_thread_id() # Return initial state for Gradio interface (matching output order & type) return ( "", # instructions (Textbox) "", # job_desc (Textbox) [], # conversation (Chatbot messages) None, # pdf_uploader (File) "Professional & Confident", # tone_selector (Dropdown default value) new_thread # thread (State) ) # --------------------------- # supabase_module.py # --------------------------- import os from datetime import datetime from dotenv import load_dotenv from supabase import create_client import fitz # PyMuPDF from typing import List, Optional # Load env variables load_dotenv() SUPABASE_URL = os.getenv("SUPABASE_URL") SUPABASE_KEY = os.getenv("SUPABASE_KEY") supabase = create_client(SUPABASE_URL, SUPABASE_KEY) MAX_FILES = 2 # --------------------------- # DB Functions # --------------------------- def add_file_to_db(file_name: str, file_path: str): """Extract text from PDF and insert/update in Supabase.""" text = "" with fitz.open(file_path) as doc: for page in doc: text += page.get_text() supabase.table("uploaded_files").upsert({ "file_name": file_name, "file_text": text, "upload_date": datetime.now().isoformat() }, on_conflict="file_name").execute() enforce_max_files() def enforce_max_files(): """Ensure only MAX_FILES are stored in DB, delete oldest if needed.""" try: data = supabase.table("uploaded_files").select("id, file_name, upload_date").execute() files = data.data if len(files) > MAX_FILES: for file in sorted(files, key=lambda x: x["upload_date"])[:len(files)-MAX_FILES]: supabase.table("uploaded_files").delete().eq("id", file["id"]).execute() except Exception as e: print("[Supabase] Error enforcing max files:", e) def get_files_from_db() -> List[dict]: """Return all uploaded files, latest first.""" try: data = supabase.table("uploaded_files").select("file_name, file_text, upload_date").execute() files = data.data if data.data else [] files.sort(key=lambda x: x["upload_date"], reverse=True) return files except Exception as e: print("[Supabase] Error fetching files:", e) return [] def extract_pdf_data() -> str: """Concatenate text from all uploaded PDFs.""" text = "" for file in get_files_from_db(): text += file['file_text'] print(f"[PDF Extraction] Preview: {text[:50]}...") return text # --------------------------- # Gradio Functions # --------------------------- def upload_files(files) -> List[str]: """Handle uploaded files from Gradio and store in Supabase.""" if not files: return refresh_file_list() for f in files: file_name = os.path.basename(f.name) add_file_to_db(file_name, f.name) return refresh_file_list() def refresh_file_list() -> List[str]: """Return file list for display in UI.""" files_data = get_files_from_db() return [f"{f['file_name']} (Uploaded: {f['upload_date']})" for f in files_data] async def generate_proposal(job_description: str, instructions: str, tone: str, history: list, thread: str): """Main function to generate proposal using compiled graph and PDF data.""" print(f"[Generate Proposal] thread_id={thread}, tone={tone}") user_message = {"role": "user", "content": instructions} portfolio_text = extract_pdf_data() state = { "proposal_text": "", "user_tone": tone, "instructions": instructions, "messages": user_message, "selected_tone": tone, "user_portfolio_text": portfolio_text, "job_desc": job_description, "count_loop": 0 } config = {"configurable": {"thread_id": thread}} result = await compiled_graph.ainvoke(state, config=config) proposal_text = result["proposal_text"] new_history = history + [user_message, {"role": "assistant", "content": proposal_text}] return new_history, result def get_summary_from_memory(saved_state: dict) -> str: """Generate conversation summary using MemorySaver and ConversationSummaryMemory.""" if not saved_state or "messages" not in saved_state: return "" from langchain.memory import ConversationSummaryMemory llm = ChatOpenAI(model="gpt-4o-mini") summary_memory = ConversationSummaryMemory(llm=llm) user_msg = None for msg in saved_state.get("messages", []): if isinstance(msg, HumanMessage): user_msg = msg.content elif isinstance(msg, AIMessage) and user_msg is not None: summary_memory.save_context({"input": user_msg}, {"output": msg.content}) user_msg = None summary_final = summary_memory.load_memory_variables({})["history"] print(f"[Summary] {summary_final[:100]}...") return summary_final # --------------------------- # Session Finalization # --------------------------- def finish_session(thread_id: str, saved_state: dict): """ Finalize a session by generating a conversation summary and saving it to Supabase. """ print(f"[Session] Finishing session for thread_id={thread_id}") # Generate summary summary = get_summary_from_memory(saved_state) print(f"[Session] Generated summary preview: {summary[:100]}...") # Save summary to Supabase try: supabase.table("summaries").insert({ "session_id": thread_id, "summary": summary, "created_at": datetime.now().isoformat() }).execute() print(f"✅ Conversation summarized and saved for thread {thread_id}.") except Exception as e: print(f"[Session] Error saving summary to Supabase: {e}") #---------------------- #-------GRADIO APP----- #----------------------- import gradio as gr # -------- Gradio UI -------- with gr.Blocks(css=""" #left-panel {width: 200px;} #conversation-box {height: 400px; overflow-y: auto;} """) as demo: thread = gr.State(make_thread_id()) saved_state=gr.State() with gr.Row(): # Left side panel with gr.Column(elem_id="left-panel"): gr.Markdown("### 📂 Session") refresh_btn = gr.Button("🔄 New Session") finish_btn = gr.Button("🏁 Summary") # Center conversation + inputs with gr.Column(scale=3): conversation = gr.Chatbot(label="AI Proposal Conversation", elem_id="conversation-box" ,type="messages") job_desc = gr.Textbox(label="Job Description", placeholder="Paste the job description here", lines=4) instructions = gr.Textbox(label="Instructions", placeholder="Add any special instructions here", lines=3) submit_btn = gr.Button("Generate Proposal") # Right controls with gr.Column(scale=1): tone_selector = gr.Dropdown(["Professional & Confident","Friendly & Conversational","Persuasive & Client-Focused","Creative & Narrative-Driven","Analytical & Problem-Solver"],value="Professional & Confident",label="Tone") uploader = gr.File(label="Upload Files", file_types=[".pdf"], file_count="multiple") file_list = gr.List(label="Files in Database", value=refresh_file_list()) # -------- Event Bindings -------- uploader.change(upload_files, inputs=uploader, outputs=file_list) submit_btn.click( fn=generate_proposal, inputs=[job_desc, instructions, tone_selector, conversation, thread], outputs=[conversation,saved_state] ) refresh_btn.click( fn=reset, inputs=[thread], outputs=[instructions, job_desc, conversation, uploader, tone_selector, thread] ) finish_btn.click( fn=finish_session, inputs=[thread,saved_state], outputs=[] ) if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)) )