Spaces:
Sleeping
Sleeping
ALLOUNE
commited on
Commit
·
0f73722
1
Parent(s):
17dcd11
add logic
Browse files- app.py +22 -3
- requirements.txt +4 -1
- src/__pycache__/processor.cpython-310.pyc +0 -0
- src/processor.py +82 -0
app.py
CHANGED
|
@@ -1,8 +1,27 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
from src.processor import *
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
class Input(BaseModel):
|
| 10 |
+
text1 : List
|
| 11 |
+
text2 : List
|
| 12 |
|
| 13 |
+
class Output(BaseModel):
|
| 14 |
+
matrix : List
|
| 15 |
+
|
| 16 |
+
@app.post("/process", response_model=Output)
|
| 17 |
+
def process(payload: Input):
|
| 18 |
+
saoke_spec = text_to_saoke(payload.text1)
|
| 19 |
+
saoke_patent = text_to_saoke(payload.text2)
|
| 20 |
+
|
| 21 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 22 |
+
|
| 23 |
+
embeddings1, embeddings2 = text_to_embeddings(saoke_spec, saoke_patent, model)
|
| 24 |
+
|
| 25 |
+
matrix = embeddings_to_matrix(embeddings1, embeddings2)
|
| 26 |
+
print({"matrix": matrix})
|
| 27 |
+
return {"matrix": matrix}
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
| 3 |
+
google.genai
|
| 4 |
+
sentence_transformers
|
| 5 |
+
pydantic
|
| 6 |
+
python-dotenv
|
src/__pycache__/processor.cpython-310.pyc
ADDED
|
Binary file (3.39 kB). View file
|
|
|
src/processor.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google.genai import Client, types
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
def text_to_saoke(data):
|
| 10 |
+
saoke_list = []
|
| 11 |
+
for d in data:
|
| 12 |
+
prompt = get_prompt()
|
| 13 |
+
prompt += d
|
| 14 |
+
result = send_request(prompt)
|
| 15 |
+
saoke_list.append(result)
|
| 16 |
+
return saoke_list
|
| 17 |
+
|
| 18 |
+
def text_to_embeddings(list1, list2, model):
|
| 19 |
+
embeddings1 = []
|
| 20 |
+
for l1 in list1:
|
| 21 |
+
embeddings1.append(model.encode(l1))
|
| 22 |
+
embeddings2 = []
|
| 23 |
+
for l2 in list2:
|
| 24 |
+
embeddings2.append(model.encode(l2))
|
| 25 |
+
|
| 26 |
+
return embeddings1, embeddings2
|
| 27 |
+
|
| 28 |
+
def embeddings_to_matrix(embeddings1, embeddings2):
|
| 29 |
+
matrix = []
|
| 30 |
+
for e1 in embeddings1:
|
| 31 |
+
for e2 in embeddings2:
|
| 32 |
+
cosim = util.cos_sim(e1, e2)
|
| 33 |
+
matrix.append(cosim)
|
| 34 |
+
matrix = np.array(matrix).reshape(len(embeddings1), len(embeddings2))
|
| 35 |
+
return matrix.tolist()
|
| 36 |
+
|
| 37 |
+
def send_request(prompt):
|
| 38 |
+
client = Client(api_key=os.getenv("GEMINI_API_KEY"))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
response = client.models.generate_content(
|
| 42 |
+
model="gemini-2.5-flash",
|
| 43 |
+
contents=prompt,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return response.text
|
| 47 |
+
|
| 48 |
+
def get_prompt():
|
| 49 |
+
prompt = """
|
| 50 |
+
You are well educated in S-A-O-K-E decomposition methodology applied to patents:
|
| 51 |
+
**Subject (S):** The entity that performs the action (e.g., device, user, system).
|
| 52 |
+
**Action (A):** This represents the specific intervention, process, or method that the invention performs. It describes what the invention *does* to or with specific objects or systems (e.g., transmits, applies, mixes).
|
| 53 |
+
**Object (O):** The entity or target that the action is performed upon (e.g., signal, data, mixture).
|
| 54 |
+
**Knowledge (K):** This is the body of technical and scientific information that underpins the invention. It is the knowledge that is necessary to design, implement, and operate the action successfully.
|
| 55 |
+
**Effect (E):** This refers to the outcome, result, or consequence of the action. It describes the benefit, improvement, or new capability that the invention provides.
|
| 56 |
+
the entire invention can be mapped as a linked set of S-A–O-K–E units. For example:
|
| 57 |
+
Step 1: (S₁, A₁, O₁, K₁) → E₁
|
| 58 |
+
Step 2: (S₂,A₂, O₂, K₂=E₁+...) → E₂
|
| 59 |
+
...and so on.
|
| 60 |
+
|
| 61 |
+
You mission is to help the user write and analyse their ideas, concept, inventions, problems etc... in the form of S-A-O-K-E.
|
| 62 |
+
You must output a JSON object containings all of the SAOKE:
|
| 63 |
+
|
| 64 |
+
{
|
| 65 |
+
"subject": "Mobile payment app",
|
| 66 |
+
"action": "interlaces affine and non-linear lookup tables",
|
| 67 |
+
"object": "AES state bytes",
|
| 68 |
+
"knowledge": "space-hard SPNbox design using table incompressibility theory",
|
| 69 |
+
"effect": "prevents code-lifting and key-extraction even under full memory disclosure."
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"subject": "DRM client",
|
| 73 |
+
"action": "randomizes table encodings on every re-installation",
|
| 74 |
+
"object": "symmetric key material",
|
| 75 |
+
"knowledge": "PUF-bound whitening keys with Even-Mansour construction",
|
| 76 |
+
"effect": "renders stolen binaries unusable on non-bound devices."
|
| 77 |
+
},...
|
| 78 |
+
|
| 79 |
+
### Document
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
return prompt
|