import gradio as gr import os import requests import json from typing import Dict, List # Configuration for HuggingFace Spaces - no environment variables needed # --- Configuration --- # Using the working Groq backend deployed on Modal MODAL_BACKEND_URL = "https://gebhardt-wolfh--patent-architect-groq-fastapi-app.modal.run" # --- Agent Personalities for Enhanced Demo --- AGENT_PERSONALITIES = { "Prior Art Agent": { "personality": "🕵️ Skeptical Detective", "thinking_style": "🔍 Hmm, let me dig deeper into this...", "catchphrase": "I've seen this before, but is it really the same?", "bias": "Find everything that could invalidate this patent", "negotiation_weight": 0.25 }, "Invention Summary Agent": { "personality": "📝 Technical Storyteller", "thinking_style": "✍️ How can I make this crystal clear...", "catchphrase": "Every invention has a story to tell", "bias": "Maximize technical clarity and innovation narrative", "negotiation_weight": 0.20 }, "Figure Drafter Agent": { "personality": "🎨 Visual Architect", "thinking_style": "🖼️ A picture is worth a thousand claims...", "catchphrase": "If you can't draw it, you can't patent it", "bias": "Ensure visual completeness and technical accuracy", "negotiation_weight": 0.15 }, "Claims Drafter Agent": { "personality": "⚖️ Legal Strategist", "thinking_style": "🧠 What's the broadest defensible scope...", "catchphrase": "Let's make this bulletproof!", "bias": "Balance broad protection with grantability", "negotiation_weight": 0.40 } } # IP Opportunity Categories IP_OPPORTUNITIES = [ {"type": "Adjacent Innovation", "icon": "🔗", "description": "Related inventions in the same field"}, {"type": "Cross-Industry Application", "icon": "🌐", "description": "Same tech, different industries"}, {"type": "Method Patents", "icon": "⚙️", "description": "Process and manufacturing methods"}, {"type": "Combination Patents", "icon": "🧩", "description": "Your invention + existing tech"}, {"type": "Defensive Patents", "icon": "🛡️", "description": "Block competitor strategies"} ] def create_agent_negotiation_display(agent_responses: List[Dict]) -> str: """Creates a visual display of agent negotiation and consensus building.""" html = """

🤝 Multi-Agent Strategy Negotiation

""" total_weight = sum(AGENT_PERSONALITIES[agent["name"]]["negotiation_weight"] for agent in agent_responses if agent["name"] in AGENT_PERSONALITIES) for agent in agent_responses: if agent["name"] in AGENT_PERSONALITIES: personality = AGENT_PERSONALITIES[agent["name"]] weight = personality["negotiation_weight"] influence = (weight / total_weight) * 100 if total_weight > 0 else 0 html += f"""
{personality['personality']} {influence:.1f}% influence

{personality['thinking_style']}

Position: {personality['bias']}

"{personality['catchphrase']}"

""" # Add consensus meter consensus_level = 85 # Simulated consensus consensus_color = "#28a745" if consensus_level >= 80 else "#ffc107" if consensus_level >= 60 else "#dc3545" html += f"""
📊 Negotiation Outcome
{consensus_level}% Consensus

Strategic Recommendation: Balanced approach with {consensus_level}% agent agreement

""" return html def create_ip_opportunity_cards(invention_type: str) -> str: """Generates IP opportunity cards based on invention type.""" # Simulate discovered opportunities based on invention keywords opportunities = [] if "smart" in invention_type.lower() or "iot" in invention_type.lower(): opportunities.extend([ {"type": "Adjacent Innovation", "score": 92, "description": "Smart home integration protocols", "market_size": "$2.3B"}, {"type": "Method Patents", "score": 87, "description": "Data processing and analytics methods", "market_size": "$890M"}, {"type": "Cross-Industry Application", "score": 83, "description": "Healthcare monitoring applications", "market_size": "$1.2B"} ]) elif "coffee" in invention_type.lower() or "temperature" in invention_type.lower(): opportunities.extend([ {"type": "Adjacent Innovation", "score": 89, "description": "Smart beverage ecosystem", "market_size": "$1.8B"}, {"type": "Combination Patents", "score": 85, "description": "IoT + phase-change materials", "market_size": "$650M"}, {"type": "Method Patents", "score": 78, "description": "Temperature control algorithms", "market_size": "$420M"} ]) else: opportunities.extend([ {"type": "Adjacent Innovation", "score": 88, "description": "Related technical applications", "market_size": "$1.5B"}, {"type": "Cross-Industry Application", "score": 82, "description": "Alternative market applications", "market_size": "$980M"}, {"type": "Defensive Patents", "score": 76, "description": "Competitive blocking strategy", "market_size": "$340M"} ]) html = """

💡 Discovered IP Opportunities

""" for i, opp in enumerate(opportunities): score = opp["score"] color = "#28a745" if score >= 85 else "#ffc107" if score >= 75 else "#17a2b8" # Find matching opportunity type opp_info = next((o for o in IP_OPPORTUNITIES if o["type"] == opp["type"]), IP_OPPORTUNITIES[0]) html += f"""
{opp_info['icon']} {score}/100
{opp['type']}

{opp['description']}

Market: {opp['market_size']}

""" html += """

🎯 Strategic Insight: Found {total} high-value patent opportunities worth ${total_value}B+ in market potential

""".format( total=len(opportunities), total_value=round(sum(float(o["market_size"].replace("$", "").replace("B", "").replace("M", "")) for o in opportunities if "B" in o["market_size"]), 1) ) return html def format_patent_section(agent_name: str, content: str, thought: str = None, sources: List = None, image_urls: List = None) -> str: """Enhanced patent section formatting with agent personalities.""" if agent_name in AGENT_PERSONALITIES: personality = AGENT_PERSONALITIES[agent_name] icon = personality["personality"].split()[0] # Get emoji formatted = f"### {icon} {agent_name}\n\n" # Add personality-driven thinking process if thought: formatted += f"""
🧠 Agent Thinking Process

{personality['thinking_style']}

{thought.replace(chr(10), '
')}

"{personality['catchphrase']}"

""" formatted += content # Add agent bias and strategy formatted += f"""
🎯 Agent Strategy: {personality['bias']}
""" else: # Fallback for unknown agents formatted = f"### 🤖 {agent_name}\n\n" if thought: formatted += f"
🧠 Analysis Process

{thought.replace(chr(10), '
')}

\n\n" formatted += content # Add sources if available if sources: formatted += "\n\n**Prior Art Sources:**\n" for source in sources: if source.get('url'): formatted += f"- [{source.get('title', 'Patent Source')}]({source.get('url')})\n" return formatted def run_patent_architect_in_ui(invention_disclosure): """Enhanced UI function with agent personalities and IP discovery.""" if not invention_disclosure: # Reset to initial state if input is cleared outputs = { "opportunities": gr.HTML.update(value="

💡 IP Opportunities

Enter invention to discover patent opportunities

"), "negotiation": gr.HTML.update(value="

🤝 Agent Negotiation

Agents will negotiate optimal strategy

"), "prior_art": gr.Markdown.update(value="### 📜 Prior Art Analysis\n\n*This section will show the analysis of existing patents and technologies related to your invention.*"), "summary": gr.Markdown.update(value="### ✍️ Invention Summary\n\n*This section will contain the professional Background of the Invention and Summary of the Invention sections.*"), "figures": gr.Markdown.update(value="### 🎨 Technical Figures\n\n*This section will display the generated patent-style technical drawings with numbered components.*"), "claims": gr.Markdown.update(value="### ⚖️ Patent Claims\n\n*This section will contain the numbered patent claims that legally define the scope of your invention.*"), "status": "⚖️ Patent Architect v2 - Ready for strategic IP discovery", } yield list(outputs.values()) return if not MODAL_BACKEND_URL: error_msg = "⚠️ **Backend Configuration Required**\n\nPlease deploy the Patent Architect backend and update the `MODAL_BACKEND_URL` configuration." yield error_msg, error_msg, error_msg, error_msg, "❌ Configuration Error" return # Initialize all sections with enhanced features opportunities_html = create_ip_opportunity_cards(invention_disclosure) negotiation_html = "

🤝 Agent Negotiation

Preparing multi-agent strategy session...

" prior_art_section = "### 📜 Prior Art Analysis\n\n*🕵️ Skeptical Detective is searching patent databases...*" summary_section = "### ✍️ Invention Summary\n\n*📝 Technical Storyteller is awaiting prior art analysis...*" figures_section = "### 🎨 Technical Figures\n\n*🎨 Visual Architect is awaiting invention summary...*" claims_section = "### ⚖️ Patent Claims\n\n*⚖️ Legal Strategist is awaiting technical figures...*" status = "🚀 Starting Enhanced Patent Architect with IP Discovery..." yield opportunities_html, negotiation_html, prior_art_section, summary_section, figures_section, claims_section, status try: print(f"Connecting to Patent Architect backend: {MODAL_BACKEND_URL}") with requests.post( f"{MODAL_BACKEND_URL}/generate_patent", json={"invention_disclosure": invention_disclosure}, timeout=1800, # 30 minute timeout for full patent generation stream=True ) as response: response.raise_for_status() for line in response.iter_lines(): if line: decoded_line = line.decode('utf-8') if decoded_line.startswith('data: '): try: event = json.loads(decoded_line[6:]) event_status = event.get("status") if event_status == "starting": status = event.get("log", "Starting...") elif event_status == "running": status = event.get("log", "Processing...") elif event_status == "update": data = event.get("data", {}) step_info = event.get("step", "") section_type = data.get("section_type") agent_name = data.get("agent_name", "Patent Agent") content = data.get("content", "") thought = data.get("thought", "") sources = data.get("sources", []) image_urls = data.get("image_urls", []) formatted_content = format_patent_section(agent_name, content, thought, sources, image_urls) # Update the appropriate section based on the agent's designated section_type if section_type == "prior_art": prior_art_section = formatted_content summary_section = "### ✍️ Invention Summary\n\n*📝 Technical Storyteller is crafting your invention story...*" # Show negotiation visualization agent_responses = [{"name": agent_name}] negotiation_html = create_agent_negotiation_display(agent_responses) elif section_type == "summary": summary_section = formatted_content figures_section = "### 🎨 Technical Figures\n\n*🎨 Visual Architect is designing technical drawings...*" # Update negotiation with more agents agent_responses = [{"name": "Prior Art Agent"}, {"name": agent_name}] negotiation_html = create_agent_negotiation_display(agent_responses) elif section_type == "figures": figures_section = formatted_content claims_section = "### ⚖️ Patent Claims\n\n*⚖️ Legal Strategist is optimizing claim scope...*" # Update negotiation with more agents agent_responses = [{"name": "Prior Art Agent"}, {"name": "Invention Summary Agent"}, {"name": agent_name}] negotiation_html = create_agent_negotiation_display(agent_responses) elif section_type == "claims": claims_section = formatted_content # Final negotiation with all agents agent_responses = [ {"name": "Prior Art Agent"}, {"name": "Invention Summary Agent"}, {"name": "Figure Drafter Agent"}, {"name": agent_name} ] negotiation_html = create_agent_negotiation_display(agent_responses) # Enhanced status with agent personality if agent_name in AGENT_PERSONALITIES: personality = AGENT_PERSONALITIES[agent_name]["personality"] status = f"✅ **Step {step_info} Complete** - {personality} has finished their analysis" else: status = f"✅ **Step {step_info} Complete** - {agent_name} finished" elif event_status == "assembling": status = event.get("log", "Assembling patent application...") elif event_status == "complete": status = "🎉 **Patent Application Complete!** - Ready for review and filing" yield opportunities_html, negotiation_html, prior_art_section, summary_section, figures_section, claims_section, status except json.JSONDecodeError as e: print(f"Could not decode JSON from stream: {decoded_line}") continue except requests.exceptions.RequestException as e: error_msg = f"❌ **Connection Error:** {str(e)}\n\nPlease check that the Patent Architect backend is deployed and accessible." status = "❌ Connection Failed" yield opportunities_html, negotiation_html, prior_art_section, summary_section, figures_section, claims_section, status with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="⚖️ Patent Architect v2") as demo: gr.HTML("""

⚖️ Patent Architect AI v2

AI IP Strategy Platform with Multi-Agent Intelligence

IP Discovery • Agent Negotiation • Strategic Claims • Competitive Intelligence

🚀 Beyond Patents: Strategic IP Portfolio Generation

""") gr.Markdown(""" ## 🎯 Next-Generation Patent Intelligence **Patent Architect v2** goes beyond simple patent drafting - it's an AI IP Strategy Platform: ### 🤖 **Multi-Agent Intelligence System** - **🕵️ Skeptical Detective** (Prior Art) - "I've seen this before, but is it really the same?" - **📝 Technical Storyteller** (Summary) - "Every invention has a story to tell" - **🎨 Visual Architect** (Figures) - "If you can't draw it, you can't patent it" - **⚖️ Legal Strategist** (Claims) - "Let's make this bulletproof!" ### 💡 **Strategic IP Discovery** - **IP Opportunity Mining** - Discovers 3-5 additional patent opportunities worth $1B+ market potential - **Multi-Agent Negotiation** - Agents debate optimal claim strategy with 85%+ consensus - **Competitive Intelligence** - Maps white spaces competitors haven't claimed - **Portfolio Generation** - Creates strategic patent families, not just single applications **The WOW Factor:** Watch agents with distinct personalities negotiate your optimal IP strategy in real-time! """) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 🔍 Describe Your Invention") invention_input = gr.Textbox( lines=12, label="Invention Disclosure", placeholder="""Provide a detailed description of your invention including: • What problem does it solve? • How does it work (key components, process flow)? • What makes it novel or different from existing solutions? • What are the main benefits and advantages? • Any specific technical details or mechanisms? Example: "A smart coffee mug that maintains perfect temperature using phase-change materials and app control, solving the problem of coffee getting cold too quickly while providing personalized temperature preferences..." """, info="The more detailed your description, the better your patent application will be." ) generate_btn = gr.Button("🚀 Discover IP Opportunities & Generate Strategic Patents", variant="primary", size="lg") status_display = gr.Textbox( label="Status", interactive=False, value="⚖️ Patent Architect v2 - Ready for strategic IP discovery" ) gr.Markdown("---") # Strategic Intelligence Dashboard gr.Markdown("## 🧠 AI Strategy Intelligence") with gr.Row(): with gr.Column(): opportunities_display = gr.HTML("

💡 IP Opportunities

Patent opportunities will appear here

") with gr.Column(): negotiation_display = gr.HTML("

🤝 Agent Negotiation

Multi-agent consensus will appear here

") gr.Markdown("---") gr.Markdown("## 📋 Optimized Patent Application") with gr.Tabs(): with gr.TabItem("📜 Prior Art Analysis"): prior_art_output = gr.Markdown( value="### 📜 Prior Art Analysis\n\n*This section will show the analysis of existing patents and technologies related to your invention.*" ) with gr.TabItem("✍️ Background & Summary"): summary_output = gr.Markdown( value="### ✍️ Invention Summary\n\n*This section will contain the professional Background of the Invention and Summary of the Invention sections.*" ) with gr.TabItem("🎨 Technical Figures"): figures_output = gr.Markdown( value="### 🎨 Technical Figures\n\n*This section will display the generated patent-style technical drawings with numbered components.*" ) with gr.TabItem("⚖️ Patent Claims"): claims_output = gr.Markdown( value="### ⚖️ Patent Claims\n\n*This section will contain the numbered patent claims that legally define the scope of your invention.*" ) generate_btn.click( fn=run_patent_architect_in_ui, inputs=[invention_input], outputs=[opportunities_display, negotiation_display, prior_art_output, summary_output, figures_output, claims_output, status_display] ) gr.Examples( [ ["A smart pill dispenser that uses computer vision to verify correct medication, sends alerts for missed doses, and connects to healthcare providers for monitoring compliance."], ["A modular vertical farming system with AI-controlled LED lighting that adapts spectrum and intensity based on plant growth stage, optimizing yield while reducing energy consumption."], ["A wearable device that monitors micro-expressions and vocal patterns to provide real-time feedback on presentation skills and confidence levels during public speaking."], ], inputs=[invention_input], label="💡 Example Invention Disclosures" ) gr.HTML(""" """) if __name__ == "__main__": print("⚖️ PATENT ARCHITECT - Frontend") print(f"✅ Connecting to backend: {MODAL_BACKEND_URL}") demo.queue().launch( share=True, favicon_path=None, # Disable favicon to prevent 404s show_error=False, # Hide error messages in UI quiet=True, # Reduce console output debug=False # Disable debug mode to reduce console noise )