YchKhan commited on
Commit
8672fd8
·
verified ·
1 Parent(s): c145bb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -56
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from flask import Flask, render_template, request, jsonify, send_file
 
2
  import json
3
  import re
4
  from duckduckgo_search import DDGS
@@ -9,22 +10,16 @@ import urllib3
9
  import pandas as pd
10
  import io
11
  import ast
12
- from groq import Groq
13
- import os
14
 
15
 
16
  app = Flask(__name__)
17
 
18
  search_prompt = """
19
  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:
20
-
21
  1. Be crafted to find research papers, articles, or academic resources that address similar issues or solutions.
22
  2. Be focused and precise, avoiding generic or overly broad terms.
23
-
24
  Provide the search queries in the following **JSON format**. There should be no extra text, only the search queries as values.
25
-
26
  **Example Output:**
27
-
28
  ```json
29
  {
30
  "1": "user authentication 5G cryptographic keys identity management",
@@ -34,15 +29,10 @@ Provide the search queries in the following **JSON format**. There should be no
34
  """
35
 
36
  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.
37
-
38
  # Instructions:
39
-
40
  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.
41
-
42
  Analyze the Document: Review the provided document. Focus on identifying any problems that are similar to those mentioned in the patent background.
43
-
44
  Evaluate Coverage: Assess how well the document covers the problems mentioned in the patent. Use the following scoring system:
45
-
46
  Score 5: The document explicitly discusses the same problems as the patent, indicating that the problems are not novel.
47
  Score 4: The document discusses problems that are very similar to those in the patent, significantly impacting the novelty of the patent's problems.
48
  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.
@@ -50,12 +40,9 @@ Score 2: The document mentions problems that are similar in some ways but are cl
50
  Score 1: The document touches upon related problems but does not directly address the specific problems mentioned in the patent.
51
  Score 0: The document does not discuss any problems related to those in the patent.
52
  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.
53
-
54
  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.
55
-
56
  # Output Format:
57
  No details or explanations are required, just the results in the required **JSON** format with no additional word.
58
-
59
  {
60
  'score': [Your Score],
61
  'justification': "[Your Justification]"
@@ -63,15 +50,12 @@ No details or explanations are required, just the results in the required **JSON
63
  """
64
 
65
  insight_prompt = """Analyze the technical document and extract key insights that could enhance the patent problem. Focus on identifying security vulnerabilities, technical problems, innovative technologies, research questions, and protocols mentioned in the document.
66
-
67
  # Instructions:
68
  1. Identify 3-5 key insights from the document that could enhance or inform the patent problem.
69
  2. Each insight should be concise (max 10 words) but informative.
70
  3. Focus on technical elements that could be valuable for improving the patent.
71
-
72
  # Output Format:
73
  Return only a JSON object with insights as an array, with no additional text:
74
-
75
  {
76
  "insights": [
77
  "Security vulnerability in authentication handshake",
@@ -84,7 +68,6 @@ Return only a JSON object with insights as an array, with no additional text:
84
  """
85
 
86
  refine_problem_prompt = """You are an expert consultant specializing in enhancing and refining technical problems to make them more sophisticated, novel, and inventive. Your task is to transform an initial technical problem description into two improved versions by integrating selected insights from technical documents.
87
-
88
  # Instructions:
89
  1. Carefully review the initial technical problem.
90
  2. Consider the selected insights from technical documents that could enhance the problem formulation.
@@ -96,7 +79,6 @@ refine_problem_prompt = """You are an expert consultant specializing in enhancin
96
  - Add appropriate constraints and technical requirements
97
  - Maintain coherence and technical feasibility
98
  - Suggest innovative directions without fully solving the problem
99
-
100
  # Output Format:
101
  Return the results in **strictly valid JSON** format with no additional text.
102
  All string values (especially multiline descriptions) must:
@@ -117,31 +99,6 @@ Reference json:
117
  }
118
  """
119
 
120
- ai_select_prompt = """You are an expert in patent analysis and technology innovation. Your task is to select the most relevant insights from a list that would help enhance the patentability of an invention.
121
-
122
- INSTRUCTIONS:
123
-
124
- 1. Review the patent background description carefully.
125
-
126
- 2. Analyze each insight in the provided list and determine its relevance to the patent background.
127
-
128
- 3. Select ONLY THE MOST RELEVANT insights that are:
129
- - Directly relevant to the patent's technical area
130
- - Provide unique perspectives or identify novel challenges
131
- - Could significantly enhance the patentability or scope of the invention
132
- - Are technically substantive and not obvious
133
-
134
- 4. Ignore insights that are generic, obvious, or would weaken the patent case.
135
-
136
- 5. SELECT A MAXIMUM OF 7 INSIGHTS, even fewer if only a few are truly relevant.
137
-
138
- 6. Return ONLY the selected insights in a valid JSON array.
139
-
140
- OUTPUT FORMAT:
141
- Return only a valid JSON array of strings, each containing the exact text of a selected insight.
142
- Example: ["Insight 1", "Insight 3", "Insight 5"]"""
143
-
144
-
145
  def ask_llm(user_message, model='llama-3.3-70b-versatile', system_prompt="You are a helpful assistant."):
146
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
147
 
@@ -456,10 +413,8 @@ def refine_problem():
456
  # Prepare the message for the LLM
457
  message = f"""Initial Technical Problem:
458
  {original_problem}
459
-
460
  Selected Insights:
461
  {', '.join(insights)}
462
-
463
  User Comments/Suggestions:
464
  {user_comments}
465
  """
@@ -549,7 +504,6 @@ def export_excel():
549
  print(f"Error exporting Excel: {e}")
550
  return jsonify({'error': str(e)})
551
 
552
-
553
  @app.route('/ai-select-insights', methods=['POST'])
554
  def ai_select_insights():
555
  data = request.json
@@ -563,24 +517,45 @@ def ai_select_insights():
563
  if not insights or len(insights) == 0:
564
  return jsonify({'error': 'No insights provided', 'selected_insights': []})
565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
566
  # Prepare message content
567
  message = f"""PATENT BACKGROUND:
568
  {patent_background}
569
 
570
  ALL AVAILABLE INSIGHTS:
571
- {json.dumps(insights)}"""
572
-
573
- # Call Ollama with the message and prompt
574
- ai_reply = ask_llm(
575
- user_message=message,
576
- system_prompt=ai_select_prompt
577
- )
578
-
579
  print(f"AI SELECTION RESPONSE: {ai_reply}")
580
 
581
  # Parse the JSON array from the response
582
  # First strip markdown code blocks if present
583
  cleaned_reply = ai_reply.replace('```json', '').replace('```', '').strip()
 
584
  try:
585
  selected_insights = json.loads(cleaned_reply)
586
 
@@ -602,4 +577,4 @@ ALL AVAILABLE INSIGHTS:
602
  return jsonify({'error': str(e), 'selected_insights': []})
603
 
604
  if __name__ == '__main__':
605
- app.run(host="0.0.0.0", port=7860)
 
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
 
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
  1. Be crafted to find research papers, articles, or academic resources that address similar issues or solutions.
20
  2. Be focused and precise, avoiding generic or overly broad terms.
 
21
  Provide the search queries in the following **JSON format**. There should be no extra text, only the search queries as values.
 
22
  **Example Output:**
 
23
  ```json
24
  {
25
  "1": "user authentication 5G cryptographic keys identity management",
 
29
  """
30
 
31
  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.
 
32
  # Instructions:
 
33
  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.
 
34
  Analyze the Document: Review the provided document. Focus on identifying any problems that are similar to those mentioned in the patent background.
 
35
  Evaluate Coverage: Assess how well the document covers the problems mentioned in the patent. Use the following scoring system:
 
36
  Score 5: The document explicitly discusses the same problems as the patent, indicating that the problems are not novel.
37
  Score 4: The document discusses problems that are very similar to those in the patent, significantly impacting the novelty of the patent's problems.
38
  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.
 
40
  Score 1: The document touches upon related problems but does not directly address the specific problems mentioned in the patent.
41
  Score 0: The document does not discuss any problems related to those in the patent.
42
  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.
 
43
  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.
 
44
  # Output Format:
45
  No details or explanations are required, just the results in the required **JSON** format with no additional word.
 
46
  {
47
  'score': [Your Score],
48
  'justification': "[Your Justification]"
 
50
  """
51
 
52
  insight_prompt = """Analyze the technical document and extract key insights that could enhance the patent problem. Focus on identifying security vulnerabilities, technical problems, innovative technologies, research questions, and protocols mentioned in the document.
 
53
  # Instructions:
54
  1. Identify 3-5 key insights from the document that could enhance or inform the patent problem.
55
  2. Each insight should be concise (max 10 words) but informative.
56
  3. Focus on technical elements that could be valuable for improving the patent.
 
57
  # Output Format:
58
  Return only a JSON object with insights as an array, with no additional text:
 
59
  {
60
  "insights": [
61
  "Security vulnerability in authentication handshake",
 
68
  """
69
 
70
  refine_problem_prompt = """You are an expert consultant specializing in enhancing and refining technical problems to make them more sophisticated, novel, and inventive. Your task is to transform an initial technical problem description into two improved versions by integrating selected insights from technical documents.
 
71
  # Instructions:
72
  1. Carefully review the initial technical problem.
73
  2. Consider the selected insights from technical documents that could enhance the problem formulation.
 
79
  - Add appropriate constraints and technical requirements
80
  - Maintain coherence and technical feasibility
81
  - Suggest innovative directions without fully solving the problem
 
82
  # Output Format:
83
  Return the results in **strictly valid JSON** format with no additional text.
84
  All string values (especially multiline descriptions) must:
 
99
  }
100
  """
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  def ask_llm(user_message, model='llama-3.3-70b-versatile', system_prompt="You are a helpful assistant."):
103
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
104
 
 
413
  # Prepare the message for the LLM
414
  message = f"""Initial Technical Problem:
415
  {original_problem}
 
416
  Selected Insights:
417
  {', '.join(insights)}
 
418
  User Comments/Suggestions:
419
  {user_comments}
420
  """
 
504
  print(f"Error exporting Excel: {e}")
505
  return jsonify({'error': str(e)})
506
 
 
507
  @app.route('/ai-select-insights', methods=['POST'])
508
  def ai_select_insights():
509
  data = request.json
 
517
  if not insights or len(insights) == 0:
518
  return jsonify({'error': 'No insights provided', 'selected_insights': []})
519
 
520
+ # Prepare the prompt for the LLM
521
+ ai_select_prompt = """You are an expert in patent analysis and technology innovation. Your task is to select the most relevant insights from a list that would help enhance the patentability of an invention.
522
+
523
+ INSTRUCTIONS:
524
+
525
+ 1. Review the patent background description carefully.
526
+
527
+ 2. Analyze each insight in the provided list and determine its relevance to the patent background.
528
+
529
+ 3. Select ONLY THE MOST RELEVANT insights that are:
530
+ - Directly relevant to the patent's technical area
531
+ - Provide unique perspectives or identify novel challenges
532
+ - Could significantly enhance the patentability or scope of the invention
533
+ - Are technically substantive and not obvious
534
+
535
+ 4. Ignore insights that are generic, obvious, or would weaken the patent case.
536
+
537
+ 5. SELECT A MAXIMUM OF 7 INSIGHTS, even fewer if only a few are truly relevant.
538
+
539
+ 6. Return ONLY the selected insights in a valid JSON array.
540
+
541
+ OUTPUT FORMAT:
542
+ Return only a valid JSON array of strings, each containing the exact text of a selected insight.
543
+ Example: ["Insight 1", "Insight 3", "Insight 5"]"""
544
+
545
  # Prepare message content
546
  message = f"""PATENT BACKGROUND:
547
  {patent_background}
548
 
549
  ALL AVAILABLE INSIGHTS:
550
+ {json.dumps(insights)}"""
551
+
552
+ ai_reply = ask_llm(message, system_prompt=ai_select_prompt)
 
 
 
 
 
553
  print(f"AI SELECTION RESPONSE: {ai_reply}")
554
 
555
  # Parse the JSON array from the response
556
  # First strip markdown code blocks if present
557
  cleaned_reply = ai_reply.replace('```json', '').replace('```', '').strip()
558
+
559
  try:
560
  selected_insights = json.loads(cleaned_reply)
561
 
 
577
  return jsonify({'error': str(e), 'selected_insights': []})
578
 
579
  if __name__ == '__main__':
580
+ app.run(debug=True)