Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +43 -46
inference.py
CHANGED
|
@@ -1,54 +1,51 @@
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
from search_utils import web_search
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
|
|
|
|
|
|
| 10 |
model = EvoTransformerV22()
|
| 11 |
model.load_state_dict(torch.load("evo_hellaswag.pt", map_location="cpu"))
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
{"role": "user", "content": prompt}
|
| 50 |
-
]
|
| 51 |
-
)
|
| 52 |
-
return response['choices'][0]['message']['content']
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return f"⚠️ GPT error: {str(e)}"
|
|
|
|
| 1 |
+
# inference.py
|
| 2 |
+
|
| 3 |
import torch
|
| 4 |
+
import torch.nn.functional as F
|
|
|
|
| 5 |
from transformers import AutoTokenizer
|
| 6 |
+
from model import EvoTransformerV22
|
| 7 |
+
import openai
|
|
|
|
| 8 |
|
| 9 |
+
# Load tokenizer
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 11 |
+
|
| 12 |
+
# Load EvoTransformer model
|
| 13 |
model = EvoTransformerV22()
|
| 14 |
model.load_state_dict(torch.load("evo_hellaswag.pt", map_location="cpu"))
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
+
# GPT-3.5 API
|
| 18 |
+
openai.api_key = "sk-..." # Replace with your key
|
| 19 |
+
|
| 20 |
+
def get_evo_response(question, option1, option2):
|
| 21 |
+
pair1 = f"{question} {option1}"
|
| 22 |
+
pair2 = f"{question} {option2}"
|
| 23 |
+
|
| 24 |
+
def score(pair):
|
| 25 |
+
encoded = tokenizer(pair, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
logits = model(encoded["input_ids"])
|
| 28 |
+
prob = torch.sigmoid(logits).item()
|
| 29 |
+
return prob
|
| 30 |
+
|
| 31 |
+
score1 = score(pair1)
|
| 32 |
+
score2 = score(pair2)
|
| 33 |
+
|
| 34 |
+
better = option1 if score1 > score2 else option2
|
| 35 |
+
confidence = max(score1, score2)
|
| 36 |
+
|
| 37 |
+
return better, confidence, score1, score2
|
| 38 |
+
|
| 39 |
+
def get_gpt_response(question, option1, option2):
|
| 40 |
+
prompt = (
|
| 41 |
+
f"Question: {question}\n"
|
| 42 |
+
f"Option 1: {option1}\n"
|
| 43 |
+
f"Option 2: {option2}\n"
|
| 44 |
+
f"Which option makes more sense and why?"
|
| 45 |
+
)
|
| 46 |
+
response = openai.ChatCompletion.create(
|
| 47 |
+
model="gpt-3.5-turbo",
|
| 48 |
+
messages=[{"role": "user", "content": prompt}],
|
| 49 |
+
temperature=0.7
|
| 50 |
+
)
|
| 51 |
+
return response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|