YchKhan commited on
Commit
f7f62a4
·
verified ·
1 Parent(s): 499f453

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +299 -0
  2. requirements.txt +34 -0
  3. templates/index.html +765 -0
app.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify, send_file
2
+ import ollama
3
+ import json
4
+ import re
5
+ from duckduckgo_search import DDGS
6
+ import requests
7
+ from bs4 import BeautifulSoup
8
+ import fitz # PyMuPDF
9
+ import urllib3
10
+ import pandas as pd
11
+ import io
12
+ import ast
13
+
14
+
15
+ app = Flask(__name__)
16
+
17
+ search_prompt = """
18
+ The user will provide a detailed description of a technical problem they are trying to solve in the context of intellectual property (IP) and patents. Your task is to generate some (2 to 5) highly specific and relevant search queries for Google, aimed at finding research papers closely related to the user's problem. Each search query should:
19
+
20
+ 1. Be crafted to find research papers, articles, or academic resources that address similar issues or solutions.
21
+ 2. Be focused and precise, avoiding generic or overly broad terms.
22
+
23
+ Provide the search queries in the following **JSON format**. There should be no extra text, only the search queries as values.
24
+
25
+ **Example Output:**
26
+
27
+ ```json
28
+ {
29
+ "1": "user authentication 5G cryptographic keys identity management",
30
+ "2": "5G authentication security issues cryptography 3GPP key management"
31
+ }
32
+ ```
33
+ """
34
+
35
+ infringement_prompt = """You are an expert assistant designed to evaluate the novelty and inventiveness of patents by comparing them with existing documents. Your task is to analyze the background of a given patent and the first page of a related document to determine how well the document covers the problems mentioned in the patent.
36
+
37
+ # Instructions:
38
+
39
+ Understand the Patent Background: Carefully read and comprehend the background information provided for the patent. Identify the key problems that the patent aims to address.
40
+
41
+ Analyze the Document: Review the provided document. Focus on identifying any problems that are similar to those mentioned in the patent background.
42
+
43
+ Evaluate Coverage: Assess how well the document covers the problems mentioned in the patent. Use the following scoring system:
44
+
45
+ Score 5: The document explicitly discusses the same problems as the patent, indicating that the problems are not novel.
46
+ Score 4: The document discusses problems that are very similar to those in the patent, significantly impacting the novelty of the patent's problems.
47
+ Score 3: The document mentions problems that are somewhat similar to those in the patent, but the coverage is not extensive enough to fully block the novelty of the patent's problems.
48
+ Score 2: The document mentions problems that are similar in some ways but are clearly different from those in the patent.
49
+ Score 1: The document touches upon related problems but does not directly address the specific problems mentioned in the patent.
50
+ Score 0: The document does not discuss any problems related to those in the patent.
51
+ Provide a Score: Based on your analysis, provide a score from 0 to 5 indicating how well the document covers the problems mentioned in the patent.
52
+
53
+ Justify Your Score: Briefly explain the reasoning behind your score, highlighting specific similarities or differences between the problems discussed in the patent and the document.
54
+
55
+ # Output Format:
56
+ No details or explainations are required, just the results in the required **JSON** format with no additional word.
57
+
58
+ {
59
+ 'score': [Your Score],
60
+ 'justification': "[Your Justification]"
61
+ }
62
+ """
63
+
64
+
65
+ def ask_ollama(user_message, model='gemma3:1b', system_prompt=search_prompt):
66
+ response = ollama.chat(model=model, messages=[
67
+ {
68
+ "role": "system",
69
+ "content": system_prompt
70
+ },
71
+ {
72
+ "role": "user",
73
+ "content": user_message
74
+ }
75
+ ])
76
+ ai_reply = response['message']['content']
77
+ print(f"AI REPLY json:\n{ai_reply}")
78
+
79
+ # Process the response to ensure we return valid JSON
80
+ try:
81
+ # First, try to parse it directly in case it's already valid JSON
82
+ print(f"AI REPLY:\n{ai_reply}")
83
+ return ast.literal_eval(ai_reply.replace('json\n', '').replace('```', ''))
84
+ except:
85
+ print(f"ERROR:\n{e}")
86
+ # If it's not valid JSON, try to extract JSON from the text
87
+ return {
88
+ "1": "Error parsing response. Please try again.",
89
+ "2": "Error parsing response. Please try again."
90
+ }
91
+
92
+ def search_web(topic, max_references=5, data_type="pdf"):
93
+ """Search the web using DuckDuckGo and return results."""
94
+ doc_list = []
95
+ with DDGS(verify=False) as ddgs:
96
+ i = 0
97
+ for r in ddgs.text(topic, region='wt-wt', safesearch='On', timelimit='n'):
98
+ if i >= max_references:
99
+ break
100
+ doc_list.append({"type": data_type, "title": r['title'], "body": r['body'], "url": r['href']})
101
+ i += 1
102
+ return doc_list
103
+
104
+ def analyze_pdf_novelty(patent_background, url, data_type="pdf"):
105
+ """Extract first page text from PDF or background from patent and evaluate novelty"""
106
+ try:
107
+ # Disable SSL warnings
108
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
109
+ # Extract text based on the type
110
+ if data_type == "pdf":
111
+ headers = {
112
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
113
+ "Accept": "application/pdf"
114
+ }
115
+
116
+ response = requests.get(url, headers=headers, timeout=20, verify=False)
117
+ if response.status_code != 200:
118
+ print(f"Failed to download PDF (status code: {response.status_code})")
119
+ return {"error": f"Failed to download PDF (status code: {response.status_code})"}
120
+
121
+ # Extract first page text
122
+ try:
123
+ pdf_document = fitz.open(stream=response.content, filetype="pdf")
124
+ if pdf_document.page_count == 0:
125
+ return {"error": "PDF has no pages"}
126
+
127
+ first_page = pdf_document.load_page(0)
128
+ text = first_page.get_text()
129
+ except Exception as e:
130
+ return {"error": f"Error processing PDF: {str(e)}"}
131
+
132
+ elif data_type == "patent":
133
+ # Extract background from patent
134
+ print("extract from patent")
135
+ try:
136
+ response = requests.get(url, timeout=20, verify=False)
137
+ if response.status_code != 200:
138
+ print(f"Failed to access patent (status code: {response.status_code})")
139
+ return {"error": f"Failed to access patent (status code: {response.status_code})"}
140
+ content = response.content.decode('utf-8').replace("\n", "")
141
+ soup = BeautifulSoup(content, 'html.parser')
142
+ section = soup.find('section', itemprop='description', itemscope='')
143
+ matches = re.findall(r"background(.*?)(?:summary|description of the drawing)", str(section), re.DOTALL | re.IGNORECASE)
144
+ if matches:
145
+ text = BeautifulSoup(matches[0], "html.parser").get_text(separator=" ").strip()
146
+ else:
147
+ text = "Background section not found in patent."
148
+ except Exception as e:
149
+ return {"error": f"Error processing patent: {str(e)}"}
150
+ elif data_type == "web":
151
+ try:
152
+ headers = {
153
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
154
+ "Accept": "application/pdf"
155
+ }
156
+ response = requests.get(url, headers=headers, timeout=20, verify=False)
157
+ response.raise_for_status()
158
+ soup = BeautifulSoup(response.text, 'html.parser')
159
+ full_text = soup.get_text()
160
+ text = re.sub(r'\n+', ' ', full_text)[:5000]
161
+ except requests.RequestException as e:
162
+ return {"error": f"Error fetching the page: {str(e)}"}
163
+ else:
164
+ return {"error": "Unknown document type"}
165
+
166
+ # Analyze with Ollama
167
+ result = ask_ollama(
168
+ user_message=f"Patent background:\n{patent_background}\n\nDocument first page:\n{text}",
169
+ system_prompt=infringement_prompt
170
+ )
171
+
172
+ return result
173
+
174
+ except Exception as e:
175
+ return {"error": f"Error: {str(e)}"}
176
+
177
+ @app.route('/')
178
+ def home():
179
+ return render_template('index.html')
180
+
181
+ @app.route('/chat', methods=['POST'])
182
+ def chat():
183
+ user_message = request.form.get('message')
184
+ ai_reply = ask_ollama(user_message)
185
+ return jsonify({'reply': ai_reply})
186
+
187
+ @app.route('/search', methods=['POST'])
188
+ def search():
189
+ query = request.form.get('query')
190
+ pdf_checked = request.form.get('pdfOption') == 'true'
191
+ patent_checked = request.form.get('patentOption') == 'true'
192
+ web_checked = request.form.get('webOption') == 'true' or request.form.get('webOption') == 'on'
193
+
194
+ if not query:
195
+ return jsonify({'error': 'No query provided', 'results': []})
196
+
197
+ all_results = []
198
+
199
+ try:
200
+ # Handle various combinations
201
+ if pdf_checked:
202
+ pdf_query = f"{query} filetype:pdf"
203
+ pdf_results = search_web(pdf_query, max_references=5, data_type="pdf")
204
+ all_results.extend(pdf_results)
205
+
206
+ if patent_checked:
207
+ patent_query = f"{query} site:patents.google.com"
208
+ patent_results = search_web(patent_query, max_references=5, data_type="patent")
209
+ all_results.extend(patent_results)
210
+
211
+ if web_checked:
212
+ # For web, we don't add anything to the query
213
+ web_results = search_web(query, max_references=5, data_type="web")
214
+ all_results.extend(web_results)
215
+
216
+ # If nothing is checked, default to web search
217
+ if not (pdf_checked or patent_checked or web_checked):
218
+ web_results = search_web(query, max_references=5, data_type="web")
219
+ all_results.extend(web_results)
220
+
221
+ return jsonify({'results': all_results})
222
+ except Exception as e:
223
+ print(f"Error performing search: {e}")
224
+ return jsonify({'error': str(e), 'results': []})
225
+
226
+ @app.route('/analyze', methods=['POST'])
227
+ def analyze():
228
+ data = request.json
229
+ if not data or 'patent_background' not in data or 'pdf_url' not in data:
230
+ return jsonify({'error': 'Missing required parameters', 'result': None})
231
+
232
+ try:
233
+ patent_background = data['patent_background']
234
+ url = data['pdf_url']
235
+ data_type = data.get('data_type', 'pdf') # Default to pdf if not specified
236
+
237
+ result = analyze_pdf_novelty(patent_background, url, data_type)
238
+ return jsonify({'result': result})
239
+ except Exception as e:
240
+ print(f"Error analyzing document: {e}")
241
+ return jsonify({'error': str(e), 'result': None})
242
+
243
+ @app.route('/export-excel', methods=['POST'])
244
+ def export_excel():
245
+ try:
246
+ data = request.json
247
+ if not data or 'tableData' not in data:
248
+ return jsonify({'error': 'No table data provided'})
249
+
250
+ # Create pandas DataFrame from the data
251
+ df = pd.DataFrame(data['tableData'])
252
+
253
+ # Get the user query
254
+ user_query = data.get('userQuery', 'No query provided')
255
+
256
+ # Create a BytesIO object to store the Excel file
257
+ output = io.BytesIO()
258
+
259
+ # Create Excel file with xlsxwriter engine
260
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
261
+ # Write the data to a sheet named 'Results'
262
+ df.to_excel(writer, sheet_name='Results', index=False)
263
+
264
+ # Get workbook and worksheet objects
265
+ workbook = writer.book
266
+ worksheet = writer.sheets['Results']
267
+
268
+ # Add a sheet for the query
269
+ query_sheet = workbook.add_worksheet('Query')
270
+ query_sheet.write(0, 0, 'Patent Query')
271
+ query_sheet.write(1, 0, user_query)
272
+
273
+ # Adjust column widths
274
+ for i, col in enumerate(df.columns):
275
+ # Get maximum column width
276
+ max_len = max(
277
+ df[col].astype(str).map(len).max(),
278
+ len(col)
279
+ ) + 2
280
+ # Set column width (limit to 100 to avoid issues)
281
+ worksheet.set_column(i, i, min(max_len, 100))
282
+
283
+ # Seek to the beginning of the BytesIO object
284
+ output.seek(0)
285
+
286
+ # Return the Excel file
287
+ return send_file(
288
+ output,
289
+ mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
290
+ as_attachment=True,
291
+ download_name='patent_search_results.xlsx'
292
+ )
293
+
294
+ except Exception as e:
295
+ print(f"Error exporting Excel: {e}")
296
+ return jsonify({'error': str(e)})
297
+
298
+ if __name__ == '__main__':
299
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.9.0
3
+ beautifulsoup4==4.13.3
4
+ blinker==1.9.0
5
+ certifi==2025.1.31
6
+ charset-normalizer==3.4.1
7
+ click==8.1.8
8
+ duckduckgo_search==7.5.2
9
+ Flask==3.1.0
10
+ h11==0.14.0
11
+ httpcore==1.0.7
12
+ httpx==0.28.1
13
+ idna==3.10
14
+ itsdangerous==2.2.0
15
+ Jinja2==3.1.6
16
+ lxml==5.3.1
17
+ MarkupSafe==3.0.2
18
+ numpy==2.2.4
19
+ ollama==0.4.7
20
+ pandas==2.2.3
21
+ primp==0.14.0
22
+ pydantic==2.10.6
23
+ pydantic_core==2.27.2
24
+ PyMuPDF==1.25.4
25
+ python-dateutil==2.9.0.post0
26
+ pytz==2025.1
27
+ requests==2.32.3
28
+ six==1.17.0
29
+ sniffio==1.3.1
30
+ soupsieve==2.6
31
+ typing_extensions==4.12.2
32
+ tzdata==2025.1
33
+ urllib3==2.3.0
34
+ Werkzeug==3.1.3
templates/index.html ADDED
@@ -0,0 +1,765 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Patentability</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
8
+ <script>
9
+ function generateQueries(event) {
10
+ event.preventDefault();
11
+
12
+ const userInput = document.getElementById('userInput').value.trim();
13
+ if (!userInput) {
14
+ alert('Please enter a technical problem description');
15
+ return;
16
+ }
17
+
18
+ // Show loading indicator
19
+ document.getElementById('loadingIndicator').style.display = 'block';
20
+ // Hide results if they were previously shown
21
+ document.getElementById('resultsContainer').style.display = 'none';
22
+
23
+ // Send message to Flask backend
24
+ fetch('/chat', {
25
+ method: 'POST',
26
+ body: new URLSearchParams({ 'message': userInput }),
27
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
28
+ })
29
+ .then(response => response.json())
30
+ .then(data => {
31
+ // Hide loading indicator
32
+ document.getElementById('loadingIndicator').style.display = 'none';
33
+
34
+ try {
35
+ // Parse the JSON string from the LLM response
36
+ let jsonData;
37
+ // First check if data.reply is already an object
38
+ if (typeof data.reply === 'object') {
39
+ jsonData = data.reply;
40
+ } else {
41
+ // Try to extract JSON if it's a string possibly with markdown code blocks
42
+ const jsonString = data.reply.replace(/```json|```/g, '').trim();
43
+ jsonData = JSON.parse(jsonString);
44
+ }
45
+
46
+ // Clear existing queries
47
+ const queriesContainer = document.getElementById('queriesContainer');
48
+ queriesContainer.innerHTML = '';
49
+
50
+ // Add each query from the response
51
+ Object.keys(jsonData).forEach(key => {
52
+ addQueryField(jsonData[key]);
53
+ });
54
+
55
+ // Show results container
56
+ document.getElementById('resultsContainer').style.display = 'block';
57
+ } catch (error) {
58
+ console.error('Error parsing JSON:', error);
59
+ alert('There was an error processing the response. Please try again.');
60
+ }
61
+ })
62
+ .catch(error => {
63
+ document.getElementById('loadingIndicator').style.display = 'none';
64
+ console.error('Error:', error);
65
+ alert('There was an error communicating with the server. Please try again.');
66
+ });
67
+ }
68
+
69
+ // Add a new query field with the given text (or empty if not provided)
70
+ function addQueryField(queryText = '') {
71
+ const queriesContainer = document.getElementById('queriesContainer');
72
+ const queryIndex = queriesContainer.children.length + 1;
73
+
74
+ // Create main container for this query item
75
+ const queryItem = document.createElement('div');
76
+ queryItem.className = 'query-item';
77
+
78
+ // Create container for query field and buttons
79
+ const queryContainer = document.createElement('div');
80
+ queryContainer.className = 'query-container';
81
+
82
+ // Input field
83
+ const queryField = document.createElement('input');
84
+ queryField.type = 'text';
85
+ queryField.className = 'query-field';
86
+ queryField.value = queryText;
87
+ queryField.placeholder = `Search Query ${queryIndex}`;
88
+
89
+ // Delete button
90
+ const deleteButton = document.createElement('button');
91
+ deleteButton.type = 'button';
92
+ deleteButton.className = 'action-button delete-button';
93
+ deleteButton.textContent = 'Delete';
94
+ deleteButton.onclick = function() {
95
+ queriesContainer.removeChild(queryItem);
96
+ // Update the placeholder numbers for remaining queries
97
+ updateQueryIndices();
98
+ };
99
+ // Search button
100
+ const searchButton = document.createElement('button');
101
+ searchButton.type = 'button';
102
+ searchButton.className = 'action-button search-button';
103
+ searchButton.textContent = 'Search';
104
+ searchButton.onclick = function() {
105
+ performSearch(queryField.value, queryItem);
106
+ };
107
+
108
+ // Add elements to container
109
+ queryContainer.appendChild(queryField);
110
+ queryContainer.appendChild(searchButton);
111
+ queryContainer.appendChild(deleteButton);
112
+
113
+ // Create loading indicator for this search
114
+ const searchLoading = document.createElement('div');
115
+ searchLoading.className = 'search-loading';
116
+ searchLoading.textContent = 'Searching... Please wait.';
117
+
118
+ // Create table for results
119
+ const resultsTable = document.createElement('table');
120
+ resultsTable.className = 'results-table';
121
+
122
+ // Add table header
123
+ const tableHeader = document.createElement('thead');
124
+ const headerRow = document.createElement('tr');
125
+
126
+ const typeHeader = document.createElement('th');
127
+ typeHeader.textContent = 'Type';
128
+
129
+ const titleHeader = document.createElement('th');
130
+ titleHeader.textContent = 'Title';
131
+
132
+ const bodyHeader = document.createElement('th');
133
+ bodyHeader.textContent = 'Description';
134
+
135
+ const urlHeader = document.createElement('th');
136
+ urlHeader.textContent = 'URL';
137
+
138
+ const scoreHeader = document.createElement('th');
139
+ scoreHeader.textContent = 'Score';
140
+
141
+ const justificationHeader = document.createElement('th');
142
+ justificationHeader.textContent = 'Justification';
143
+
144
+ headerRow.appendChild(typeHeader);
145
+ headerRow.appendChild(titleHeader);
146
+ headerRow.appendChild(bodyHeader);
147
+ headerRow.appendChild(urlHeader);
148
+ headerRow.appendChild(scoreHeader);
149
+ headerRow.appendChild(justificationHeader);
150
+ tableHeader.appendChild(headerRow);
151
+
152
+ // Add table body
153
+ const tableBody = document.createElement('tbody');
154
+
155
+ resultsTable.appendChild(tableHeader);
156
+ resultsTable.appendChild(tableBody);
157
+
158
+ // Add all elements to the query item
159
+ queryItem.appendChild(queryContainer);
160
+ queryItem.appendChild(searchLoading);
161
+ queryItem.appendChild(resultsTable);
162
+
163
+ // Add container to the queries list
164
+ queriesContainer.appendChild(queryItem);
165
+ }
166
+
167
+ // Perform search and update the results table
168
+ function performSearch(query, queryItemElement) {
169
+ if (!query.trim()) {
170
+ alert('Please enter a search query');
171
+ return;
172
+ }
173
+
174
+ const loadingElement = queryItemElement.querySelector('.search-loading');
175
+ const tableElement = queryItemElement.querySelector('.results-table');
176
+ const tableBody = tableElement.querySelector('tbody');
177
+
178
+ // Show loading and hide previous results
179
+ loadingElement.style.display = 'block';
180
+ tableElement.style.display = 'none';
181
+
182
+ // Clear previous results
183
+ tableBody.innerHTML = '';
184
+
185
+ // Get checkbox values
186
+ const pdfChecked = document.getElementById('pdfOption').checked;
187
+ const patentChecked = document.getElementById('patentOption').checked;
188
+ const webChecked = document.getElementById('webOption').checked;
189
+
190
+
191
+ // Create form data with query and checkbox values
192
+ const formData = new FormData();
193
+ formData.append('query', query);
194
+ formData.append('pdfOption', pdfChecked);
195
+ formData.append('patentOption', patentChecked);
196
+ formData.append('webOption', webChecked);
197
+
198
+
199
+ // Send search request to backend
200
+ fetch('/search', {
201
+ method: 'POST',
202
+ body: formData
203
+ })
204
+ .then(response => response.json())
205
+ .then(data => {
206
+ // Hide loading indicator
207
+ loadingElement.style.display = 'none';
208
+
209
+ if (data.results && data.results.length > 0) {
210
+ // Populate table with results
211
+ data.results.forEach(result => {
212
+ const row = document.createElement('tr');
213
+
214
+ const typeCell = document.createElement('td');
215
+ typeCell.textContent = result.type;
216
+
217
+ const titleCell = document.createElement('td');
218
+ titleCell.textContent = result.title;
219
+
220
+ const bodyCell = document.createElement('td');
221
+ bodyCell.textContent = result.body;
222
+
223
+ const urlCell = document.createElement('td');
224
+ const urlLink = document.createElement('a');
225
+ urlLink.href = result.url;
226
+ urlLink.textContent = result.url;
227
+ urlLink.className = 'url-link';
228
+ urlLink.target = '_blank';
229
+ urlCell.appendChild(urlLink);
230
+
231
+ // Add "Analyze" button to the URL cell
232
+ const analyzeButton = document.createElement('button');
233
+ analyzeButton.textContent = 'Analyze';
234
+ analyzeButton.className = 'action-button analyze-button';
235
+ analyzeButton.onclick = function(e) {
236
+ e.preventDefault();
237
+ analyzePaper(result.url, queryItemElement);
238
+ };
239
+ urlCell.appendChild(document.createElement('br'));
240
+ urlCell.appendChild(analyzeButton);
241
+
242
+ row.appendChild(typeCell);
243
+ row.appendChild(titleCell);
244
+ row.appendChild(bodyCell);
245
+ row.appendChild(urlCell);
246
+
247
+ tableBody.appendChild(row);
248
+ });
249
+
250
+ // Show the table
251
+ tableElement.style.display = 'table';
252
+ } else {
253
+ // No results
254
+ const row = document.createElement('tr');
255
+ const cell = document.createElement('td');
256
+ cell.colSpan = 4; // Updated to 4 since we now have 4 columns with the type column
257
+ cell.textContent = 'No results found.';
258
+ cell.style.textAlign = 'center';
259
+ row.appendChild(cell);
260
+ tableBody.appendChild(row);
261
+ tableElement.style.display = 'table';
262
+ }
263
+ })
264
+ .catch(error => {
265
+ loadingElement.style.display = 'none';
266
+ console.error('Error:', error);
267
+
268
+ // Show error in table
269
+ const row = document.createElement('tr');
270
+ const cell = document.createElement('td');
271
+ cell.colSpan = 4; // Updated to 4 since we now have 4 columns with the type column
272
+ cell.textContent = 'Error performing search. Please try again.';
273
+ cell.style.textAlign = 'center';
274
+ cell.style.color = 'red';
275
+ row.appendChild(cell);
276
+ tableBody.appendChild(row);
277
+ tableElement.style.display = 'table';
278
+ });
279
+ }
280
+
281
+ function analyzePaper(paperUrl, queryItemElement) {
282
+ const patentBackground = document.getElementById('userInput').value.trim();
283
+ if (!patentBackground) {
284
+ alert('Please provide a patent background in the input field');
285
+ return;
286
+ }
287
+
288
+ // Find the row containing this URL
289
+ const rows = queryItemElement.querySelectorAll('tbody tr');
290
+ let targetRow;
291
+ let documentType = 'pdf'; // Default type
292
+
293
+ for (const row of rows) {
294
+ const urlLink = row.querySelector('.url-link');
295
+ if (urlLink && urlLink.href === paperUrl) {
296
+ targetRow = row;
297
+ // Get the document type from the first cell of the row
298
+ documentType = row.cells[0].textContent.toLowerCase();
299
+ break;
300
+ }
301
+ }
302
+
303
+ if (!targetRow) {
304
+ alert('Could not find the paper in the results table');
305
+ return;
306
+ }
307
+
308
+ // Check if we already have analysis columns
309
+ let scoreCell, justificationCell;
310
+ if (targetRow.cells.length <= 4) {
311
+ // We need to create the cells
312
+ scoreCell = document.createElement('td');
313
+ scoreCell.className = 'score-cell';
314
+ scoreCell.innerHTML = '<div class="loading-spinner"></div>';
315
+
316
+ justificationCell = document.createElement('td');
317
+ justificationCell.className = 'justification-cell';
318
+ justificationCell.innerHTML = 'Analyzing...';
319
+
320
+ targetRow.appendChild(scoreCell);
321
+ targetRow.appendChild(justificationCell);
322
+ } else {
323
+ // Use existing cells
324
+ scoreCell = targetRow.cells[4];
325
+ justificationCell = targetRow.cells[5];
326
+ scoreCell.innerHTML = '<div class="loading-spinner"></div>';
327
+ justificationCell.innerHTML = 'Analyzing...';
328
+ }
329
+
330
+ // Send analysis request to backend
331
+ fetch('/analyze', {
332
+ method: 'POST',
333
+ body: JSON.stringify({
334
+ 'patent_background': patentBackground,
335
+ 'pdf_url': paperUrl,
336
+ 'data_type': documentType
337
+ }),
338
+ headers: { 'Content-Type': 'application/json' }
339
+ })
340
+ .then(response => response.json())
341
+ .then(data => {
342
+ if (data.error) {
343
+ scoreCell.innerHTML = 'Error';
344
+ justificationCell.innerHTML = data.error;
345
+ } else if (data.result) {
346
+ // Get score and justification from the result
347
+ const result = data.result;
348
+ const score = result.score !== undefined ? result.score : 'N/A';
349
+ const justification = result.justification || 'No justification provided';
350
+
351
+ // Update cells with results
352
+ scoreCell.innerHTML = score;
353
+ justificationCell.innerHTML = justification;
354
+
355
+ // Color-code the score
356
+ if (score >= 0 && score <= 5) {
357
+ const redComponent = Math.floor((score / 5) * 255);
358
+ const greenComponent = Math.floor(((5 - score) / 5) * 255);
359
+ scoreCell.style.backgroundColor = `rgb(${redComponent}, ${greenComponent}, 0)`;
360
+ scoreCell.style.color = 'white';
361
+ scoreCell.style.fontWeight = 'bold';
362
+ scoreCell.style.textAlign = 'center';
363
+ }
364
+ } else {
365
+ scoreCell.innerHTML = 'No data';
366
+ justificationCell.innerHTML = 'Analysis failed';
367
+ }
368
+ })
369
+ .catch(error => {
370
+ console.error('Error:', error);
371
+ scoreCell.innerHTML = 'Error';
372
+ justificationCell.innerHTML = 'Failed to analyze paper';
373
+ });
374
+ }
375
+
376
+ // Update the placeholder text numbers after deletions
377
+ function updateQueryIndices() {
378
+ const queryItems = document.getElementById('queriesContainer').children;
379
+ for (let i = 0; i < queryItems.length; i++) {
380
+ const queryField = queryItems[i].querySelector('.query-field');
381
+ queryField.placeholder = `Search Query ${i + 1}`;
382
+ }
383
+ }
384
+
385
+ // Get all queries as an array of strings
386
+ function getQueries() {
387
+ const queryFields = document.querySelectorAll('.query-field');
388
+ return Array.from(queryFields).map(field => field.value.trim());
389
+ }
390
+
391
+ // Analyze all unanalyzed papers in the results
392
+ function analyzeAllPapers() {
393
+ // Show loading overlay
394
+ const loadingOverlay = document.getElementById('globalLoadingOverlay');
395
+ if (loadingOverlay) loadingOverlay.style.display = 'flex';
396
+
397
+ // Get all query items
398
+ const queryItems = document.querySelectorAll('.query-item');
399
+ let totalToAnalyze = 0;
400
+ let completedAnalyses = 0;
401
+
402
+ // Count total papers that need analysis
403
+ queryItems.forEach(queryItem => {
404
+ const rows = queryItem.querySelectorAll('tbody tr');
405
+ rows.forEach(row => {
406
+ if (row.cells.length <= 4 || row.cells[4].textContent.trim() === 'Error' ||
407
+ row.cells[4].textContent.trim() === 'N/A' || row.cells[4].textContent.trim() === '') {
408
+ totalToAnalyze++;
409
+ }
410
+ });
411
+ });
412
+
413
+ if (totalToAnalyze === 0) {
414
+ alert('No papers need analysis');
415
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
416
+ return;
417
+ }
418
+
419
+ // Function to update progress
420
+ function updateProgress() {
421
+ completedAnalyses++;
422
+ if (loadingOverlay) {
423
+ const progressElement = loadingOverlay.querySelector('.progress-text');
424
+ if (progressElement) {
425
+ progressElement.textContent = `Analyzing papers: ${completedAnalyses}/${totalToAnalyze}`;
426
+ }
427
+ }
428
+
429
+ if (completedAnalyses >= totalToAnalyze) {
430
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
431
+ }
432
+ }
433
+
434
+ // Analyze each paper that needs it
435
+ queryItems.forEach(queryItem => {
436
+ const rows = queryItem.querySelectorAll('tbody tr');
437
+ rows.forEach(row => {
438
+ // Check if this row needs analysis
439
+ if (row.cells.length <= 4 || row.cells[4].textContent.trim() === 'Error' ||
440
+ row.cells[4].textContent.trim() === 'N/A' || row.cells[4].textContent.trim() === '') {
441
+
442
+ // Get the URL
443
+ const urlLink = row.querySelector('.url-link');
444
+ const documentType = row.cells[0].textContent.toLowerCase();
445
+ if (urlLink && urlLink.href) {
446
+ // Create a promise for this analysis
447
+ const promise = new Promise((resolve) => {
448
+ // Prepare for analysis
449
+ const patentBackground = document.getElementById('userInput').value.trim();
450
+
451
+ // Create score and justification cells if needed
452
+ let scoreCell, justificationCell;
453
+ if (row.cells.length <= 4) {
454
+ scoreCell = document.createElement('td');
455
+ scoreCell.className = 'score-cell';
456
+ scoreCell.innerHTML = '<div class="loading-spinner"></div>';
457
+
458
+ justificationCell = document.createElement('td');
459
+ justificationCell.className = 'justification-cell';
460
+ justificationCell.innerHTML = 'Analyzing...';
461
+
462
+ row.appendChild(scoreCell);
463
+ row.appendChild(justificationCell);
464
+ } else {
465
+ scoreCell = row.cells[4];
466
+ justificationCell = row.cells[5];
467
+ scoreCell.innerHTML = '<div class="loading-spinner"></div>';
468
+ justificationCell.innerHTML = 'Analyzing...';
469
+ }
470
+
471
+ // Send analysis request
472
+ fetch('/analyze', {
473
+ method: 'POST',
474
+ body: JSON.stringify({
475
+ 'patent_background': patentBackground,
476
+ 'pdf_url': urlLink.href,
477
+ 'data_type': documentType
478
+ }),
479
+ headers: { 'Content-Type': 'application/json' }
480
+ })
481
+ .then(response => response.json())
482
+ .then(data => {
483
+ if (data.error) {
484
+ scoreCell.innerHTML = 'Error';
485
+ justificationCell.innerHTML = data.error;
486
+ } else if (data.result) {
487
+ // Get score and justification
488
+ const result = data.result;
489
+ const score = result.score !== undefined ? result.score : 'N/A';
490
+ const justification = result.justification || 'No justification provided';
491
+
492
+ // Update cells
493
+ scoreCell.innerHTML = score;
494
+ justificationCell.innerHTML = justification;
495
+
496
+ // Color-code score
497
+ if (score >= 0 && score <= 5) {
498
+ const redComponent = Math.floor((score / 5) * 255);
499
+ const greenComponent = Math.floor(((5 - score) / 5) * 255);
500
+ scoreCell.style.backgroundColor = `rgb(${redComponent}, ${greenComponent}, 0)`;
501
+ scoreCell.style.color = 'white';
502
+ scoreCell.style.fontWeight = 'bold';
503
+ scoreCell.style.textAlign = 'center';
504
+ }
505
+ } else {
506
+ scoreCell.innerHTML = 'No data';
507
+ justificationCell.innerHTML = 'Analysis failed';
508
+ }
509
+ resolve();
510
+ })
511
+ .catch(error => {
512
+ console.error('Error:', error);
513
+ scoreCell.innerHTML = 'Error';
514
+ justificationCell.innerHTML = 'Failed to analyze paper';
515
+ resolve();
516
+ });
517
+ });
518
+
519
+ // When this analysis is done, update the progress
520
+ promise.then(() => {
521
+ updateProgress();
522
+ });
523
+ } else {
524
+ // No URL found, count as completed
525
+ updateProgress();
526
+ }
527
+ }
528
+ });
529
+ });
530
+ }
531
+
532
+ // Remove rows with failed analyses
533
+ function removeFailedAnalyses() {
534
+ const queryItems = document.querySelectorAll('.query-item');
535
+ let removedCount = 0;
536
+
537
+ queryItems.forEach(queryItem => {
538
+ const tbody = queryItem.querySelector('tbody');
539
+ const rows = Array.from(tbody.querySelectorAll('tr'));
540
+
541
+ rows.forEach(row => {
542
+ if (row.cells.length > 4) {
543
+ const scoreText = row.cells[4].textContent.trim();
544
+ if (scoreText === 'Error' || scoreText === 'N/A' || scoreText === 'No data') {
545
+ tbody.removeChild(row);
546
+ removedCount++;
547
+ }
548
+ }
549
+ });
550
+ });
551
+
552
+ alert(`Removed ${removedCount} papers with failed analyses`);
553
+ }
554
+
555
+ // Export all tables to Excel
556
+ function exportToExcel() {
557
+ // Show loading overlay
558
+ const loadingOverlay = document.getElementById('globalLoadingOverlay');
559
+ if (loadingOverlay) {
560
+ loadingOverlay.style.display = 'flex';
561
+ loadingOverlay.querySelector('.progress-text').textContent = 'Generating Excel file...';
562
+ }
563
+
564
+ try {
565
+ // Collect all data from all tables
566
+ const allData = [];
567
+ const queryItems = document.querySelectorAll('.query-item');
568
+
569
+ queryItems.forEach(queryItem => {
570
+ // Get the search query text for this table
571
+ const queryText = queryItem.querySelector('.query-field').value;
572
+
573
+ // Get all rows in this table
574
+ const rows = queryItem.querySelectorAll('tbody tr');
575
+
576
+ rows.forEach(row => {
577
+ // Skip empty rows or error rows
578
+ if (row.cells.length === 1 && row.cells[0].colSpan > 1) {
579
+ return; // Skip "No results found" rows
580
+ }
581
+
582
+ // Prepare object for this row
583
+ const rowData = {
584
+ 'Topic': queryText,
585
+ 'Type': row.cells[0].textContent,
586
+ 'Title': row.cells[1].textContent,
587
+ 'Description': row.cells[2].textContent,
588
+ 'URL': row.querySelector('.url-link')?.href || ''
589
+ };
590
+
591
+ // Add score and justification if they exist
592
+ if (row.cells.length > 4) {
593
+ rowData['Score'] = row.cells[4].textContent;
594
+ rowData['Justification'] = row.cells[5].textContent;
595
+ } else {
596
+ rowData['Score'] = '';
597
+ rowData['Justification'] = '';
598
+ }
599
+
600
+ allData.push(rowData);
601
+ });
602
+ });
603
+
604
+ // Check if we have any data
605
+ if (allData.length === 0) {
606
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
607
+ alert('No data to export. Please perform a search first.');
608
+ return;
609
+ }
610
+
611
+ // User query for context
612
+ const userQuery = document.getElementById('userInput').value;
613
+
614
+ // Send to server for Excel generation
615
+ fetch('/export-excel', {
616
+ method: 'POST',
617
+ body: JSON.stringify({
618
+ 'tableData': allData,
619
+ 'userQuery': userQuery
620
+ }),
621
+ headers: { 'Content-Type': 'application/json' }
622
+ })
623
+ .then(response => {
624
+ if (!response.ok) {
625
+ throw new Error(`Server error: ${response.status}`);
626
+ }
627
+ return response.blob();
628
+ })
629
+ .then(blob => {
630
+ // Hide loading overlay
631
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
632
+
633
+ // Create URL for the blob
634
+ const url = window.URL.createObjectURL(new Blob([blob], {
635
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
636
+ }));
637
+
638
+ // Create a link and trigger download
639
+ const a = document.createElement('a');
640
+ a.href = url;
641
+ a.download = 'patent_search_results.xlsx';
642
+ document.body.appendChild(a);
643
+ a.click();
644
+
645
+ // Clean up
646
+ window.URL.revokeObjectURL(url);
647
+ document.body.removeChild(a);
648
+ })
649
+ .catch(error => {
650
+ console.error('Error exporting to Excel:', error);
651
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
652
+ alert(`Error exporting to Excel: ${error.message}`);
653
+ });
654
+ } catch (error) {
655
+ console.error('Error preparing Excel data:', error);
656
+ if (loadingOverlay) loadingOverlay.style.display = 'none';
657
+ alert(`Error preparing data for export: ${error.message}`);
658
+ }
659
+ }
660
+ </script>
661
+ </head>
662
+ <body>
663
+ <div class="container">
664
+ <header class="header">
665
+ <div class="logo">
666
+ <div class="logo-icon">P</div>
667
+ <h1>Patentability</h1>
668
+ </div>
669
+ <p>Enter a detailed description of your technical problem to generate search queries for finding relevant research papers.</p>
670
+ </header>
671
+
672
+ <section class="card">
673
+ <form id="queryForm" onsubmit="generateQueries(event)">
674
+ <div class="form-group">
675
+ <label for="userInput">Technical Problem Description:</label>
676
+ <textarea id="userInput" placeholder="Describe your technical problem in detail..." required></textarea>
677
+ </div>
678
+
679
+ <button type="submit" class="btn btn-primary">Generate Search Queries</button>
680
+ </form>
681
+
682
+ <div id="loadingIndicator">
683
+ <div class="loading-spinner"></div>
684
+ <p>Generating search queries... Please wait.</p>
685
+ </div>
686
+ </section>
687
+
688
+ <section class="search-options">
689
+ <label>Search Options:</label>
690
+ <div class="checkbox-group">
691
+ <div class="checkbox-item">
692
+ <input type="checkbox" id="pdfOption" name="searchOptions" value="pdf" checked>
693
+ <label for="pdfOption">PDF</label>
694
+ </div>
695
+ <div class="checkbox-item">
696
+ <input type="checkbox" id="patentOption" name="searchOptions" value="patent">
697
+ <label for="patentOption">Patent</label>
698
+ </div>
699
+ <div class="checkbox-item">
700
+ <input type="checkbox" id="webOption" name="searchOptions" value="web">
701
+ <label for="webOption">Web</label>
702
+ </div>
703
+ </div>
704
+ </section>
705
+
706
+ <section id="resultsContainer">
707
+ <h2>Generated Search Queries</h2>
708
+
709
+ <div id="queriesContainer">
710
+ <!-- Query fields will be added here dynamically -->
711
+ </div>
712
+
713
+ <div class="button-container">
714
+ <button type="button" class="btn btn-secondary" onclick="addQueryField()">
715
+ <span>Add New Query</span>
716
+ </button>
717
+ </div>
718
+ </section>
719
+ </div>
720
+
721
+
722
+
723
+
724
+ <!-- Global action buttons (floating) -->
725
+ <div class="floating-buttons">
726
+ <button id="analyzeAllButton" class="btn btn-primary floating-button" title="Analyze All Unanalyzed Papers">
727
+ Analyze All
728
+ </button>
729
+ <button id="removeFailedButton" class="btn btn-danger floating-button" title="Remove Papers with Failed Analyses">
730
+ Remove Failed
731
+ </button>
732
+ <button id="exportExcelButton" class="btn btn-success floating-button" title="Export All Data to Excel">
733
+ Export to Excel
734
+ </button>
735
+ </div>
736
+
737
+ <!-- Global loading overlay -->
738
+ <div id="globalLoadingOverlay" class="loading-overlay">
739
+ <div class="loading-content">
740
+ <div class="loading-spinner"></div>
741
+ <div class="progress-text">Processing...</div>
742
+ </div>
743
+ </div>
744
+
745
+ <script>
746
+ // Add event listeners for floating buttons
747
+ document.addEventListener('DOMContentLoaded', function() {
748
+ // Analyze all button
749
+ document.getElementById('analyzeAllButton').addEventListener('click', function() {
750
+ analyzeAllPapers();
751
+ });
752
+
753
+ // Remove failed button
754
+ document.getElementById('removeFailedButton').addEventListener('click', function() {
755
+ removeFailedAnalyses();
756
+ });
757
+
758
+ // Export to Excel button
759
+ document.getElementById('exportExcelButton').addEventListener('click', function() {
760
+ exportToExcel();
761
+ });
762
+ });
763
+ </script>
764
+ </body>
765
+ </html>