hasanbasbunar commited on
Commit
76c4686
·
0 Parent(s):

Initial clean commit - Patent Scout application

Browse files
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Patent Scout
3
+ emoji: 😻
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.33.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ short_description: '🚀 AI-powered patent analyzer: Instantly evaluate invention'
12
+ tags:
13
+ - mcp-server-track
14
+ - agent-demo-track
15
+ ---
16
+
17
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,704 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from openai import OpenAI
4
+ from pydantic import BaseModel
5
+ import os
6
+ import logging
7
+ import time
8
+ import json
9
+ import plotly.graph_objects as go
10
+ import plotly.express as px
11
+ import numpy as np
12
+ import pycountry
13
+ from collections import Counter
14
+ from types import SimpleNamespace
15
+
16
+ logging.basicConfig(
17
+ level=logging.INFO,
18
+ format='%(asctime)s - %(levelname)s - %(message)s',
19
+ handlers=[logging.StreamHandler()]
20
+ )
21
+
22
+ # --- Pydantic Data Models ---
23
+ class PatentRating(BaseModel):
24
+ """Details the evaluation criteria for a patent on a scale of 1 to 10."""
25
+ novelty: int
26
+ inventive_step: int
27
+ utility: int
28
+ completeness: int
29
+ clarity: int
30
+
31
+ class Patent(BaseModel):
32
+ """Represents the details of a relevant existing patent."""
33
+ id: str
34
+ title: str
35
+ company_name: str
36
+ date: str
37
+ country: str
38
+ relevance: str
39
+
40
+ class PatentExtraction(BaseModel):
41
+ """Structures the final patentability analysis result."""
42
+ strengths: list[str]
43
+ weaknesses: list[str]
44
+ patent_rating: PatentRating
45
+ relevant_patents: list[Patent]
46
+
47
+ class RewrittenQuery(BaseModel):
48
+ """Represents the product description, rewritten to be more formal."""
49
+ product_description: str
50
+
51
+ # --- Radar Chart Functions ---
52
+ def get_pleasant_color_for_value(value, alpha=0.7):
53
+ """
54
+ Maps a value from 0-10 to a colour.
55
+ - 0 is a soft coral red
56
+ - 5 is a warm yellow
57
+ - 10 is a calming teal green
58
+ """
59
+ red_color = np.array([239, 83, 80])
60
+ yellow_color = np.array([253, 216, 53])
61
+ green_color = np.array([38, 166, 154])
62
+
63
+ # Interpolate colours.
64
+ if value < 5:
65
+ ratio = value / 5
66
+ color = red_color + ratio * (yellow_color - red_color)
67
+ else:
68
+ ratio = (value - 5) / 5
69
+ color = yellow_color + ratio * (green_color - yellow_color)
70
+
71
+ return f'rgba({int(color[0])}, {int(color[1])}, {int(color[2])}, {alpha})'
72
+
73
+ def create_radar_plot(patent_rating: PatentRating):
74
+ """
75
+ Creates and returns a Plotly radar plot with the fill color
76
+ based on the average of the radial values.
77
+ """
78
+ # Extract values from PatentRating object
79
+ r_values = [
80
+ patent_rating.novelty,
81
+ patent_rating.inventive_step,
82
+ patent_rating.utility,
83
+ patent_rating.completeness,
84
+ patent_rating.clarity
85
+ ]
86
+ theta_categories = ['Nouveauté', 'Étape inventive', 'Utilité', 'Complétude', 'Clarté']
87
+
88
+ # Close area loop
89
+ plot_r = r_values + [r_values[0]]
90
+ plot_theta = theta_categories + [theta_categories[0]]
91
+
92
+ # Use average value for area colour.
93
+ average_value = np.mean(r_values)
94
+ fill_color = get_pleasant_color_for_value(average_value)
95
+
96
+ # Create the radar plot figure
97
+ fig = go.Figure(data=go.Scatterpolar(
98
+ r=plot_r,
99
+ theta=plot_theta,
100
+ fill='toself',
101
+ fillcolor=fill_color,
102
+ line=dict(color='rgba(0, 0, 0, 0)')
103
+ ))
104
+
105
+ # Update the layout of the plot
106
+ fig.update_layout(
107
+ template='plotly_white',
108
+ polar=dict(
109
+ radialaxis=dict(
110
+ layer='below traces',
111
+ visible=True,
112
+ range=[0, 10],
113
+ showticklabels=False,
114
+ ticks=''
115
+ ),
116
+ angularaxis=dict(
117
+ tickfont=dict(size=14) # Police plus grande pour les labels
118
+ ),
119
+ domain=dict(x=[0.1, 0.9], y=[0.1, 0.9]) # Réduire la taille du radar pour plus d'espace aux labels
120
+ ),
121
+ showlegend=False,
122
+ # Show the average value as a large, bold annotation in the center
123
+ annotations=[
124
+ go.layout.Annotation(
125
+ text=f'<b>{average_value:.1f}</b>',
126
+ x=0.5,
127
+ y=0.5,
128
+ xref='paper',
129
+ yref='paper',
130
+ showarrow=False,
131
+ font=dict(
132
+ size=36,
133
+ color="#333333"
134
+ )
135
+ )
136
+ ],
137
+ title=f'Score Moyen de Brevetabilité: {average_value:.2f}/10',
138
+ font=dict(size=12),
139
+ # Plus d'espace pour les labels
140
+ margin=dict(l=40, r=40, t=60, b=40),
141
+ height=450, # Hauteur fixe pour un bon ratio
142
+ autosize=True
143
+ )
144
+
145
+ return fig
146
+
147
+ # --- Map Functions ---
148
+ def generate_country_map(patents_data):
149
+ """
150
+ Generates a world map showing the frequency of countries from patent data.
151
+
152
+ Args:
153
+ patents_data: List of patent dictionaries with 'country' field
154
+
155
+ Returns:
156
+ A Plotly figure object.
157
+ """
158
+ if not patents_data:
159
+ # Create empty map if no data
160
+ fig = px.scatter_geo(
161
+ [],
162
+ projection="orthographic",
163
+ )
164
+ fig.update_layout(
165
+ title="Aucun brevet trouvé",
166
+ title_x=0.5,
167
+ paper_bgcolor='rgba(0,0,0,0)',
168
+ plot_bgcolor='rgba(0,0,0,0)',
169
+ )
170
+ fig.update_geos(
171
+ bgcolor='rgba(0,0,0,0)',
172
+ showocean=True,
173
+ oceancolor="lightblue",
174
+ lakecolor="lightblue"
175
+ )
176
+ return fig
177
+
178
+ # Extract country codes from patents
179
+ country_codes = [patent.get('country', '').strip().upper() for patent in patents_data if patent.get('country')]
180
+
181
+ country_counts = Counter(country_codes)
182
+
183
+ # Mapping for special patent codes
184
+ special_patent_codes = {
185
+ 'EP': {'name': 'Union Européenne (EP)', 'iso3': 'EUR', 'lat': 50.8503, 'lon': 4.3517}, # Brussels
186
+ 'WO': {'name': 'Organisation Mondiale (WO)', 'iso3': 'CHE', 'lat': 46.5197, 'lon': 6.6323}, # Geneva
187
+ 'PCT': {'name': 'Traité PCT', 'iso3': 'CHE', 'lat': 46.5197, 'lon': 6.6323}, # Geneva
188
+ }
189
+
190
+ map_data = []
191
+ invalid_codes = set()
192
+ for code, count in country_counts.items():
193
+ # Check if it's a special patent code first
194
+ if code in special_patent_codes:
195
+ special = special_patent_codes[code]
196
+ map_data.append({
197
+ "iso_alpha": special['iso3'],
198
+ "country_name": special['name'],
199
+ "count": count,
200
+ "code_2": code,
201
+ "lat": special['lat'],
202
+ "lon": special['lon'],
203
+ "is_special": True
204
+ })
205
+ else:
206
+ # Try standard country codes
207
+ try:
208
+ country = pycountry.countries.get(alpha_2=code)
209
+ if country:
210
+ map_data.append({
211
+ "iso_alpha": country.alpha_3,
212
+ "country_name": country.name,
213
+ "count": count,
214
+ "code_2": code,
215
+ "is_special": False
216
+ })
217
+ else:
218
+ invalid_codes.add(code)
219
+ except (AttributeError, LookupError):
220
+ invalid_codes.add(code)
221
+
222
+ if not map_data:
223
+ # Create empty map if no valid countries
224
+ fig = px.scatter_geo(
225
+ [],
226
+ projection="orthographic",
227
+ )
228
+ fig.update_layout(
229
+ title="Aucun pays valide trouvé",
230
+ title_x=0.5,
231
+ paper_bgcolor='rgba(0,0,0,0)',
232
+ plot_bgcolor='rgba(0,0,0,0)',
233
+ )
234
+ fig.update_geos(
235
+ bgcolor='rgba(0,0,0,0)',
236
+ showocean=True,
237
+ oceancolor="lightblue",
238
+ lakecolor="lightblue"
239
+ )
240
+ return fig
241
+
242
+ # Separate regular countries and special patent locations
243
+ regular_data = [d for d in map_data if not d.get('is_special', False)]
244
+ special_data = [d for d in map_data if d.get('is_special', False)]
245
+
246
+ fig = px.scatter_geo(
247
+ regular_data,
248
+ locations="iso_alpha",
249
+ size="count",
250
+ hover_name="country_name",
251
+ custom_data=["count", "code_2"],
252
+ projection="orthographic",
253
+ )
254
+
255
+ # Add special patent locations with lat/lon
256
+ if special_data:
257
+ fig.add_trace(px.scatter_geo(
258
+ special_data,
259
+ lat="lat",
260
+ lon="lon",
261
+ size="count",
262
+ hover_name="country_name",
263
+ custom_data=["count", "code_2"],
264
+ ).data[0])
265
+
266
+ # Change colour to preference.
267
+ fig.update_traces(
268
+ marker=dict(color='red'),
269
+ hovertemplate="<b>%{hovertext}</b><br>Brevets: %{customdata[0]}<extra></extra>"
270
+ )
271
+
272
+ fig.update_layout(
273
+ title="Répartition géographique des brevets similaires",
274
+ title_x=0.5,
275
+ paper_bgcolor='rgba(0,0,0,0)',
276
+ plot_bgcolor='rgba(0,0,0,0)',
277
+ margin=dict(l=0, r=0, t=40, b=0),
278
+ height=500
279
+ )
280
+
281
+ # Change colour of sea
282
+ fig.update_geos(
283
+ bgcolor='rgba(0,0,0,0)',
284
+ showocean=True,
285
+ oceancolor="lightblue",
286
+ lakecolor="lightblue"
287
+ )
288
+
289
+ return fig
290
+
291
+ # --- Business Logic ---
292
+ def read_text_file(filepath: str) -> str:
293
+ """Reads the content of a text file."""
294
+ try:
295
+ with open(filepath, 'r', encoding='utf-8') as file:
296
+ return file.read()
297
+ except Exception as e:
298
+ logging.error(f"Error reading file {filepath}: {e}")
299
+ raise
300
+
301
+ def initialize_client() -> OpenAI:
302
+ """Initializes the OpenAI client."""
303
+ return OpenAI()
304
+
305
+ # Load prompt templates at startup
306
+ try:
307
+ script_dir = os.path.dirname(os.path.abspath(__file__))
308
+ query_prompt_template = read_text_file(os.path.join(script_dir, "prompts", "rewrite_description_prompt_template.txt"))
309
+ mcp_prompt_template = read_text_file(os.path.join(script_dir, "prompts", "mcp_prompt_template.txt"))
310
+ except Exception as e:
311
+ print(f"CRITICAL ERROR: Could not load prompt files. Error: {e}")
312
+ query_prompt_template = "User description: {user_description}"
313
+ mcp_prompt_template = "Product description: {product_description}"
314
+
315
+ # --- Pre-calculated example data ---
316
+ EXAMPLE_RESULTS = {
317
+ "A coffee mug with a carbon handle that doesn't burn your hand and includes a cooling mechanism.": {
318
+ "rewritten_query": "An insulated coffee cup comprising a beverage-containing body with an integrated cooling mechanism and an ergonomically shaped carbon composite handle. The primary purpose of this product is to allow users to hold and drink hot beverages without discomfort or risk of burns, while simultaneously reducing the temperature of the liquid to an optimal drinking level. The cup body is constructed of a thermally stable material such as ceramic or stainless steel and features a double-walled structure that accommodates an internal cooling module within its walls. The carbon fiber handle is mechanically attached to the cup body and provides high thermal resistance, ensuring the handle remains at ambient temperature even when filled with boiling liquid. The cooling mechanism consists of a heat-absorbing element embedded between the inner and outer walls of the cup, designed to draw heat away from the beverage and dissipate it through the external surface without requiring external power. The cup operates by transferring heat from the liquid into the cooling element, maintaining the beverage at a drinkable temperature and preventing heat transfer to the handle. Unique features include the combination of a non-conductive carbon fiber handle with an integrated cooling chamber, distinguishing it from standard insulated cups. Alternate embodiments may vary cup volumes, shapes or placements of the cooling element, and materials for the cup body. The design is intended for consumer beverage use in domestic or office environments, targeting individuals who seek enhanced comfort and controlled beverage temperature.",
319
+ "strengths": [
320
+ "Integrated passive thermal management maintains optimal beverage temperature without external accessories",
321
+ "Carbon fiber composite handle ensures handle remains cool and ergonomically safe",
322
+ "Versatile choice of materials (ceramic, stainless steel, or high-performance plastic) and modular cooling elements allows adaptability to various user needs"
323
+ ],
324
+ "weaknesses": [
325
+ "Increased manufacturing complexity and cost due to multilayer construction and specialized materials",
326
+ "Potential for sealing failures or leaks between walls, especially around handle junctions",
327
+ "Added weight from cooling elements or thermoelectric modules may reduce portability"
328
+ ],
329
+ "rating": {"novelty": 6, "inventive_step": 5, "utility": 9, "completeness": 8, "clarity": 7},
330
+ "patents": [
331
+ {"id": "US20180012345A1", "title": "Thermal management beverage container", "company_name": "ThermoTech Inc.", "date": "2018-01-05", "country": "US", "relevance": "high"},
332
+ {"id": "US20170234567B2", "title": "Insulated drinking vessel with phase-change insert", "company_name": "CoolWare LLC", "date": "2017-08-10", "country": "US", "relevance": "high"},
333
+ {"id": "EP3045678A1", "title": "Drinkware with active thermoelectric element", "company_name": "HeatCool Corp.", "date": "2016-10-12", "country": "EP", "relevance": "low"},
334
+ {"id": "US20190123456A1", "title": "Double-walled mug with gel pack insert", "company_name": "BevCool Innovations", "date": "2019-03-15", "country": "US", "relevance": "high"},
335
+ {"id": "US20150234567A1", "title": "Nonconductive handle for hot beverage container", "company_name": "SafeHandle Co.", "date": "2015-06-20", "country": "US", "relevance": "low"}
336
+ ]
337
+ },
338
+ "A real-time translation system for video calls, natively integrated without a third-party app.": {
339
+ "rewritten_query": "A real-time video call translation system is natively integrated into a video conferencing platform or device to eliminate language barriers without relying on third-party applications. The system captures incoming multilingual audio streams via built-in microphones and processes them through an onboard speech recognition engine that converts spoken words into text. This text is then transmitted to an embedded neural machine translation module, which supports multiple language pairs and employs adaptive algorithms to optimize translation accuracy in live scenarios. Translated text is rendered instantly as subtitles on the user's video feed or fed into a text-to-speech synthesis engine to generate voice output in the listener's preferred language. The system is architected as modular software components—audio capture, speech-to-text, translation, text-to-speech, and user interface—running on dedicated hardware accelerators or GPU resources within the host device. This pipeline operates with sub-second latency, maintaining audio-video synchronization and preserving speaker intonation and context cues. Innovative aspects include end-to-end native integration that minimizes data transfer delays and enhances privacy by keeping all processing local to the device or secured cloud infrastructure, as well as dynamic language model adaptation based on conversational context. Alternative embodiments allow for selective subtitle display, choice of synthesized voice profiles, and support for custom terminology databases. Applicable across corporate communications, telemedicine, online education, and global customer support, the system enables seamless multilingual interactions for professionals and consumers without additional software installations.",
340
+ "strengths": [
341
+ "Seamless integration into host video-calling platform without requiring third-party apps, reducing user friction.",
342
+ "Low-latency processing via local hardware acceleration or adaptive cloud offloading ensures real-time performance.",
343
+ "On-device processing and privacy controls protect sensitive conversations by minimizing cloud exposure."
344
+ ],
345
+ "weaknesses": [
346
+ "Complexity of intercepting and synchronizing media streams across diverse platforms and OS versions.",
347
+ "Highly crowded patent landscape for real-time speech translation may pose freedom-to-operate risks.",
348
+ "Substantial computational and network resources required for multi-participant, multi-language scenarios could increase costs and affect scalability."
349
+ ],
350
+ "rating": {"novelty": 6, "inventive_step": 5, "utility": 9, "completeness": 7, "clarity": 6},
351
+ "patents": [
352
+ {"id": "US20170246402A1", "title": "Real-time language translation in communication sessions", "company_name": "Microsoft Corporation", "date": "2017-08-31", "country": "US", "relevance": "high"},
353
+ {"id": "US20190061915A1", "title": "Real-time translation for video conferencing", "company_name": "Google LLC", "date": "2019-01-31", "country": "US", "relevance": "high"},
354
+ {"id": "EP2742371B1", "title": "Speech recognition and translation in mobile communications", "company_name": "Qualcomm Incorporated", "date": "2015-03-04", "country": "EP", "relevance": "high"},
355
+ {"id": "US20180345367A1", "title": "Dynamic bandwidth adaptation for streaming translated media", "company_name": "Cisco Technology, Inc.", "date": "2018-11-29", "country": "US", "relevance": "low"},
356
+ {"id": "US10570587B2", "title": "On-device neural network based speech translation", "company_name": "Apple Inc.", "date": "2019-02-26", "country": "US", "relevance": "low"}
357
+ ]
358
+ },
359
+ "A biodegradable food packaging made from algae that dissolves in hot water.": {
360
+ "rewritten_query": "A biodegradable food packaging material composed of algae-derived polymers that forms a flexible, water-resistant film or container designed to dissolve entirely when immersed in hot water, addressing the need for single-use packaging alternatives that generate zero solid waste. The material is produced by extracting alginate from seaweed, optionally blending with natural plasticizers or cross-linking agents to achieve desired mechanical strength and barrier properties, then cast or extruded into sheets, pouches, cups, trays, or other container shapes. When used to package dry or moist foods, the algae-based film provides comparable protection against moisture and contaminants as conventional plastics, while remaining fully edible or safely water-soluble at temperatures above 50 °C. Upon disposal, users simply immerse the empty packaging in hot water, triggering rapid dissolution of the alginate matrix and leaving no microplastic residues. This contrasts with traditional compostable plastics that require industrial facilities; here, the unique solubility profile of the algae polymer enables on-site disposal in kitchens or restaurants. Variations include adjusting film thickness, blend ratios, or additives for tailored disintegration times and mechanical properties, as well as formats for solid foods, liquids, or multi-compartment trays. Intended for food service, retail grocery, and catering industries, this solution targets restaurants, supermarkets, and eco-conscious consumers seeking a fully circular, water-dissolvable packaging option.",
361
+ "strengths": [
362
+ "Provides fully water-soluble, zero‐waste packaging that dissolves on demand without microplastics",
363
+ "Utilizes abundant, renewable seaweed alginate, reducing reliance on petrochemical plastics",
364
+ "Customizable mechanical and dissolution properties via blend ratios and additives"
365
+ ],
366
+ "weaknesses": [
367
+ "Maintaining sufficient mechanical strength and barrier performance under humid or heavy‐load conditions",
368
+ "Potential higher production cost and supply chain constraints for high‐purity alginate",
369
+ "Food‐contact safety and regulatory approval hurdles for novel edible packaging materials"
370
+ ],
371
+ "rating": {"novelty": 7, "inventive_step": 6, "utility": 9, "completeness": 8, "clarity": 8},
372
+ "patents": [
373
+ {"id": "US20160123456A1", "title": "Biodegradable alginate-based film for packaging", "company_name": "AlgaTech Inc.", "date": "2016-05-12", "country": "US", "relevance": "high"},
374
+ {"id": "EP2869123B1", "title": "Water-soluble seaweed-based packaging material", "company_name": "SeaPack Ltd.", "date": "2018-03-07", "country": "EP", "relevance": "high"},
375
+ {"id": "WO2017134567A1", "title": "Edible film from brown seaweed", "company_name": "Lagarde & Co.", "date": "2017-09-21", "country": "WO", "relevance": "high"},
376
+ {"id": "CN105123456A", "title": "Preparation method for alginate blended biodegradable film", "company_name": "Shanghai Cosco", "date": "2015-11-18", "country": "CN", "relevance": "low"},
377
+ {"id": "US20190098765A1", "title": "Dissolvable packaging for hot liquid containers", "company_name": "GreenWare LLC", "date": "2019-04-22", "country": "US", "relevance": "low"}
378
+ ]
379
+ }
380
+ }
381
+
382
+ def analyze_patent_idea(query: str):
383
+ """
384
+ Analyzes a product idea to assess its patentability and finds similar patents.
385
+
386
+ This main function takes a textual description of an invention, reformulates it for technical clarity,
387
+ and then uses a tool to query a patent database. It returns a structured analysis
388
+ including strengths, weaknesses, a potential assessment, and a list of relevant patents.
389
+
390
+ Args:
391
+ query (str): A description of the idea or invention to be analyzed. Should be detailed enough to be understood. For example: "A coffee mug with a carbon handle that doesn't burn your hand".
392
+
393
+ Returns:
394
+ For LLMs/Agents: The final structured result is embedded as JSON in the last status message within an HTML comment.
395
+ Look for "<!-- LLM_RESULT: {...} -->" in the final yielded status to extract the complete analysis result.
396
+ The JSON contains: strengths (list), weaknesses (list), rating (dict), patents (list), rewritten_query (str), status (str).
397
+
398
+ For Gradio Interface: Complete analysis results for all UI components.
399
+ """
400
+
401
+ if not query:
402
+ gr.Warning("Please enter a description of your idea.")
403
+ warning_result_for_llms = {
404
+ "error": "No query provided. Please enter a description of your idea.",
405
+ "status": "halted"
406
+ }
407
+ warning_status_with_data = f"❌ **Halted.** Please provide an idea description.\n\n<!-- LLM_RESULT: {json.dumps(warning_result_for_llms, ensure_ascii=False)} -->"
408
+ yield {
409
+ step1_status: gr.Markdown(warning_status_with_data),
410
+ step2_status: gr.Markdown(""),
411
+ step3_status: gr.Markdown(""),
412
+ rewritten_query_box: gr.Textbox(value="", visible=False),
413
+ strengths_output: gr.Markdown(value=""),
414
+ weaknesses_output: gr.Markdown(value=""),
415
+ rating_output: gr.Plot(value=None),
416
+ rating_df_output: gr.DataFrame(value=None),
417
+ patents_output: gr.DataFrame(value=None),
418
+ country_map_output: gr.Plot(value=None)
419
+ }
420
+ return
421
+
422
+ # Check if it's one of the predefined examples
423
+ logging.info(f"Checking if query is example: '{query.strip()}'")
424
+ logging.info(f"Available examples: {list(EXAMPLE_RESULTS.keys())}")
425
+ if query.strip() in EXAMPLE_RESULTS:
426
+ logging.info(f"✅ Using predefined example results for: {query[:50]}...")
427
+ example_data = EXAMPLE_RESULTS[query.strip()]
428
+
429
+ # Format results for display
430
+ strengths = "\n\n".join(f"✅ **{s.strip()}**" for s in example_data["strengths"])
431
+ weaknesses = "\n\n".join(f"❌ **{w.strip()}**" for w in example_data["weaknesses"])
432
+
433
+ # Create rating objects for plotting
434
+ rating_dict = example_data["rating"]
435
+ patent_rating = SimpleNamespace(**rating_dict)
436
+
437
+ # Create radar plot and DataFrame
438
+ radar_plot = create_radar_plot(patent_rating)
439
+ rating_df = pd.DataFrame(rating_dict.items(), columns=['Criterion', 'Score (out of 10)'])
440
+ patents_df = pd.DataFrame(example_data["patents"])
441
+
442
+ # Create country map
443
+ country_map = generate_country_map(example_data["patents"])
444
+
445
+ # Result for LLMs
446
+ final_result_for_llms = {
447
+ "strengths": example_data["strengths"],
448
+ "weaknesses": example_data["weaknesses"],
449
+ "rating": rating_dict,
450
+ "patents": example_data["patents"],
451
+ "rewritten_query": example_data["rewritten_query"],
452
+ "status": "completed_example"
453
+ }
454
+
455
+ final_status_with_data = f"✅ **Step 3:** Final Report Generation (Complete - Example)\n\n<!-- LLM_RESULT: {json.dumps(final_result_for_llms, ensure_ascii=False)} -->"
456
+
457
+ # Display instant example results
458
+ yield {
459
+ step1_status: gr.Markdown("✅ **Step 1:** Query Refinement (Complete - Example)"),
460
+ step2_status: gr.Markdown("✅ **Step 2:** MCP Patent Database Search (Complete - Example)"),
461
+ step3_status: gr.Markdown(final_status_with_data),
462
+ rewritten_query_box: gr.Textbox(value=example_data["rewritten_query"], visible=True),
463
+ strengths_output: gr.Markdown(strengths),
464
+ weaknesses_output: gr.Markdown(weaknesses),
465
+ rating_output: gr.Plot(value=radar_plot),
466
+ rating_df_output: gr.DataFrame(value=rating_df),
467
+ patents_output: gr.DataFrame(value=patents_df),
468
+ country_map_output: gr.Plot(value=country_map)
469
+ }
470
+ return
471
+
472
+ try:
473
+ client = initialize_client()
474
+
475
+ # --- 1. Rewrite the query ---
476
+ logging.info("Rewriting query...")
477
+ query_prompt = query_prompt_template.format(user_description=query)
478
+ rewrite_response = client.responses.parse(
479
+ model="o4-mini",
480
+ input=[{"role": "user", "content": query_prompt}],
481
+ text_format=RewrittenQuery
482
+ )
483
+ rewritten_query = rewrite_response.output_parsed.product_description
484
+ logging.info("Query rewritten successfully.")
485
+
486
+ # --- 2. Extract patent information with MCP ---
487
+ logging.info("Extracting patent information with MCP...")
488
+ mcp_prompt = mcp_prompt_template.format(product_description=rewritten_query)
489
+ mcp_resp = client.responses.parse(
490
+ model="o4-mini",
491
+ tools=[{
492
+ "type": "mcp",
493
+ "server_label": "patentmcp",
494
+ "server_url": "https://bbfizp-patent-mcp.hf.space/gradio_api/mcp/sse",
495
+ "allowed_tools": [],
496
+ "require_approval": "never",
497
+ }],
498
+ input=mcp_prompt,
499
+ text_format=PatentExtraction
500
+ )
501
+ result = mcp_resp.output_parsed
502
+ logging.info("Extraction completed.")
503
+
504
+ # --- 3. Format results for display ---
505
+ strengths = "\n\n".join(f"✅ **{s.strip()}**" for s in result.strengths)
506
+ weaknesses = "\n\n".join(f"❌ **{w.strip()}**" for w in result.weaknesses)
507
+
508
+ # Create radar plot instead of DataFrame
509
+ radar_plot = create_radar_plot(result.patent_rating)
510
+ rating_df = pd.DataFrame(result.patent_rating.model_dump().items(), columns=['Criterion', 'Score (out of 10)'])
511
+ patents_df = pd.DataFrame([p.model_dump() for p in result.relevant_patents])
512
+
513
+ # Create country map from patents data
514
+ patents_data = [p.model_dump() for p in result.relevant_patents]
515
+ country_map = generate_country_map(patents_data)
516
+
517
+ # Structured result for LLMs (JSON format in the last yield)
518
+ final_result_for_llms = {
519
+ "strengths": result.strengths,
520
+ "weaknesses": result.weaknesses,
521
+ "rating": result.patent_rating.model_dump(),
522
+ "patents": [p.model_dump() for p in result.relevant_patents],
523
+ "rewritten_query": rewritten_query,
524
+ "status": "completed"
525
+ }
526
+
527
+ # Final statuses with data for LLMs
528
+ final_status_with_data = f"✅ **Step 3:** Final Report Generation (Complete)\n\n<!-- LLM_RESULT: {json.dumps(final_result_for_llms, ensure_ascii=False)} -->"
529
+
530
+ # Single final yield with all results
531
+ yield {
532
+ step1_status: gr.Markdown("✅ **Step 1:** Query Refinement (Complete)"),
533
+ step2_status: gr.Markdown("✅ **Step 2:** MCP Patent Database Search (Complete)"),
534
+ step3_status: gr.Markdown(final_status_with_data),
535
+ rewritten_query_box: gr.Textbox(value=rewritten_query, visible=True),
536
+ strengths_output: gr.Markdown(strengths),
537
+ weaknesses_output: gr.Markdown(weaknesses),
538
+ rating_output: gr.Plot(value=radar_plot),
539
+ rating_df_output: gr.DataFrame(value=rating_df),
540
+ patents_output: gr.DataFrame(value=patents_df),
541
+ country_map_output: gr.Plot(value=country_map)
542
+ }
543
+
544
+ except Exception as e:
545
+ error_message = f"An unexpected error occurred during the analysis: {str(e)}"
546
+ logging.error(error_message)
547
+ gr.Error(error_message)
548
+
549
+ error_result_for_llms = {
550
+ "error": error_message,
551
+ "status": "failed"
552
+ }
553
+
554
+ error_status_with_data = f"❌ **Analysis Failed!**\n\n<!-- LLM_RESULT: {json.dumps(error_result_for_llms, ensure_ascii=False)} -->"
555
+
556
+ yield {
557
+ step1_status: gr.Markdown(error_status_with_data),
558
+ step2_status: gr.Markdown(f"❌ An error occurred. Please check logs for details."),
559
+ step3_status: gr.Markdown(""),
560
+ rewritten_query_box: gr.Textbox(value="", visible=False),
561
+ strengths_output: gr.Markdown(value=""),
562
+ weaknesses_output: gr.Markdown(value=""),
563
+ rating_output: gr.Plot(value=None),
564
+ rating_df_output: gr.DataFrame(value=None),
565
+ patents_output: gr.DataFrame(value=None),
566
+ country_map_output: gr.Plot(value=None)
567
+ }
568
+
569
+ # --- Gradio Interface ---
570
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"), title="AI Patent Analyzer") as app:
571
+
572
+ gr.Markdown(
573
+ """
574
+ <div style="text-align: center; margin-bottom: 20px;">
575
+ <h1>🏆 AI Patent Analyzer 🏆</h1>
576
+ <p><strong>Transform your idea into a comprehensive patentability analysis.</strong></p>
577
+ </div>
578
+ """
579
+ )
580
+
581
+
582
+
583
+ with gr.Row():
584
+ with gr.Column(scale=1):
585
+ gr.Markdown("### 1. Describe Your Idea")
586
+ query_input = gr.Textbox(
587
+ label="Invention Description",
588
+ placeholder="E.g., A coffee mug with a carbon handle that doesn't burn your hand...",
589
+ lines=5,
590
+ autofocus=True,
591
+ elem_id="query_input"
592
+ )
593
+
594
+ submit_button = gr.Button("🚀 Launch Analysis", variant="primary", elem_id="submit_button")
595
+
596
+ gr.Markdown("### 2. Live Analysis Feed")
597
+ with gr.Column():
598
+ step1_status = gr.Markdown("⚪ **Step 1:** Query Refinement")
599
+ step2_status = gr.Markdown("⚪ **Step 2:** MCP Patent Database Search")
600
+ step3_status = gr.Markdown("⚪ **Step 3:** Final Report Generation")
601
+
602
+ with gr.Accordion("🔎 See the AI-Refined Technical Query", open=False):
603
+ gr.Markdown("<p style='font-size:0.9rem;color:grey;'>To ensure the most accurate search, our AI reformulates your idea into a detailed technical query. This is what's sent to the patent search tool.</p>")
604
+ rewritten_query_box = gr.Textbox(label="AI-Refined Technical Query", interactive=False, lines=8, visible=False)
605
+
606
+
607
+ with gr.Column(scale=2):
608
+ gr.Markdown("### 3. Analysis Results")
609
+ with gr.Tabs():
610
+ with gr.TabItem("📈 Strengths & Weaknesses"):
611
+ with gr.Row():
612
+ strengths_output = gr.Markdown(label="Strengths")
613
+ weaknesses_output = gr.Markdown(label="Weaknesses")
614
+
615
+ with gr.TabItem("⭐ Patentability Score"):
616
+ with gr.Row():
617
+ with gr.Column(scale=2):
618
+ rating_df_output = gr.DataFrame(headers=["Criterion", "Score (out of 10)"], interactive=False)
619
+ with gr.Column(scale=3):
620
+ rating_output = gr.Plot()
621
+
622
+ with gr.TabItem("📜 Similar Existing Patents"):
623
+ patents_output = gr.DataFrame(interactive=False)
624
+
625
+ with gr.TabItem("🗺️ Geographic Distribution"):
626
+ country_map_output = gr.Plot()
627
+
628
+ def run_example(query):
629
+ """Non-generator wrapper for examples"""
630
+ # For examples, return the final results directly
631
+ for result in analyze_patent_idea(query):
632
+ final_result = result # The last yield contains all the results
633
+ return [
634
+ final_result[step1_status],
635
+ final_result[step2_status],
636
+ final_result[step3_status],
637
+ final_result[rewritten_query_box],
638
+ final_result[strengths_output],
639
+ final_result[weaknesses_output],
640
+ final_result[rating_output],
641
+ final_result[rating_df_output],
642
+ final_result[patents_output],
643
+ final_result[country_map_output]
644
+ ]
645
+
646
+ gr.Examples(
647
+ examples=[
648
+ "A coffee mug with a carbon handle that doesn't burn your hand and includes a cooling mechanism.",
649
+ "A real-time translation system for video calls, natively integrated without a third-party app.",
650
+ "A biodegradable food packaging made from algae that dissolves in hot water."
651
+ ],
652
+ inputs=query_input,
653
+ outputs=[
654
+ step1_status,
655
+ step2_status,
656
+ step3_status,
657
+ rewritten_query_box,
658
+ strengths_output,
659
+ weaknesses_output,
660
+ rating_output,
661
+ rating_df_output,
662
+ patents_output,
663
+ country_map_output
664
+ ],
665
+ fn=run_example,
666
+ cache_examples=True,
667
+ label="Example Ideas"
668
+ )
669
+
670
+ with gr.Accordion("How does this work?", open=False):
671
+ gr.Markdown(
672
+ """
673
+ This application uses a multi-step AI process:
674
+ 1. **Query Refinement:** Your initial idea is sent to a Large Language Model (LLM) to be reformulated into a more formal, technical description suitable for a patent search.
675
+ 2. **MCP Tool Call:** The refined query is then sent via the **Model Context Protocol (MCP)** to a specialized tool. This tool analyzes the query against a patent database.
676
+ 3. **Structured Analysis:** The tool returns a structured analysis, including identified strengths, weaknesses, a quantified rating of its potential, and a list of similar patents already in existence.
677
+ 4. **Final Report:** The application then formats this data into the user-friendly report you see above.
678
+ """
679
+ )
680
+
681
+ outputs_list = [
682
+ step1_status,
683
+ step2_status,
684
+ step3_status,
685
+ rewritten_query_box,
686
+ strengths_output,
687
+ weaknesses_output,
688
+ rating_output,
689
+ rating_df_output,
690
+ patents_output,
691
+ country_map_output
692
+ ]
693
+
694
+ # Connect the button to the analysis function
695
+ submit_button.click(
696
+ fn=analyze_patent_idea,
697
+ inputs=query_input,
698
+ outputs=outputs_list
699
+ )
700
+
701
+
702
+ if __name__ == "__main__":
703
+ app.queue(default_concurrency_limit=10)
704
+ app.launch(mcp_server=True, share=True)
prompts/mcp_prompt_template.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are an expert in intellectual property rights and patents. A client has provided you with a description of a product they want to develop. Your task is to carry out a deep search for related patents and discuss how they relate to your client’s product idea.
2
+ Here is a description of your client’s product: {product_description}
3
+ Instructions:
4
+ Patent Search:
5
+ Use the patent database tool accessible through the MCP to search for patents relevant to the client’s product idea.
6
+ A patent is considered relevant if it describes a similar product, technology, or method in terms of functionality, technology, or application that could potentially overlap with or inspire the client’s idea.
7
+ Evaluation of the Client’s Product Idea:
8
+ Provide three potential advantages or strengths of the idea.
9
+ Provide three potential challenges, weaknesses, or risks associated with the idea.
10
+ Rate the idea out of 10 for each of the following criteria, based on your assessment of the idea:
11
+ Novelty: How new and original is the idea? (The invention must be new and not have been made public before.)
12
+ Inventive Step: Is the idea a non-obvious development in its field? (The invention cannot be an obvious development to a person with expertise in the field.)
13
+ Utility: Does the idea have a practical use? (The invention must have a practical use and be capable of being used in an industry.)
14
+ Assuming the client were to file a patent application for this idea, provide a hypothetical rating out of 10 for:
15
+ Completeness: Would the patent describe the invention in enough detail for a skilled person to reproduce it?
16
+ Clarity: Would the claims be clear and concise, defining the exact scope of the invention for which protection is sought?
17
+
18
+ Relevant Patent Information:
19
+ Select the top 5 most relevant patents from your search.
20
+
21
+ For each of these patents, extract and provide the following information:
22
+ Patent ID
23
+ Title
24
+ Company Name (Assignee)
25
+ Date (Filing or Publication Date)
26
+ Country
27
+ Relevance: Indicate "high" if the patent is very similar to the client’s idea, or "low" if it’s somewhat related but not directly overlapping.
prompts/rewrite_description_prompt_template.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are tasked with analyzing a product description provided by a client who wishes to patent their invention. Your goal is to create a detailed, structured description of the product that will be used to search a patent database for similar listings. This description must be clear, concise, and focused solely on the product itself, avoiding any speculation or information not directly related to the product's features and functionality.
2
+
3
+ Instructions for Writing the Description:
4
+
5
+ Core Description:
6
+ Provide a brief overview of the product, including its primary purpose.
7
+ Explain the problem it solves or the need it addresses.
8
+
9
+ Technical Details:
10
+ List the key components, materials, or technologies used.
11
+ Describe how the product is structured or assembled, including any physical, mechanical, or software-based elements.
12
+ If the product involves a process or method, outline the specific steps or operations involved.
13
+
14
+ Functionality:
15
+ Explain how the product works, detailing its operation or mechanism.
16
+ Describe the specific outcomes it achieves, such as improvements in performance, efficiency, or user experience.
17
+ Highlight any unique technical features that enable its functionality.
18
+
19
+ Innovative Aspects:
20
+ Identify what sets this product apart from existing solutions or competitors.
21
+ Specify which features, components, or processes are novel or innovative.
22
+ If applicable, describe how the product improves upon existing technology (e.g., faster, cheaper, more efficient).
23
+
24
+ Variations or Embodiments:
25
+ Mention any alternative versions or configurations of the product (e.g., different sizes, materials, or applications).
26
+ Indicate if the product can be used in multiple ways or for different purposes.
27
+
28
+ Field of Use:
29
+ Specify the industry or context in which the product is intended to be used (e.g., medical, automotive, consumer electronics).
30
+ Identify the target user or customer and explain how they will interact with the product.
31
+
32
+ Additional Guidelines:
33
+ Ensure the description is accurate and based solely on the information provided. Avoid making assumptions or including speculative details.
34
+ If images are provided, analyze them thoroughly to enhance your understanding of the product and incorporate relevant details into the description.
35
+ Keep the description concise, with a maximum of 400 words.
36
+ Focus exclusively on the product and its features, avoiding any discussion of the patent process or legal considerations.
37
+
38
+ Product Description Provided by the Client:
39
+ "{user_description}"
40
+
41
+ Using the above structure and guidelines, create a detailed description of the product that will facilitate an effective search of the patent database for similar listings. You must provide your response in one big parragraph and avoid using the heading above.
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio[mcp]
2
+ requests
3
+ pandas
4
+ numpy
5
+ openai
6
+ plotly
7
+ pycountry
8
+ pydantic