HemanM commited on
Commit
21c89fe
·
verified ·
1 Parent(s): 2608adb

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +33 -9
inference.py CHANGED
@@ -16,28 +16,52 @@ evo_model.eval()
16
  # --- Load Tokenizer ---
17
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
18
 
19
- # --- EvoRAG+ Inference ---
20
  def evo_rag_response(query):
21
- # Step 1: get document context (from uploaded file)
22
  rag_context = retrieve(query)
23
-
24
- # Step 2: get online info (search/web)
25
  web_context = web_search(query)
26
 
27
- # Step 3: combine all into one input
28
  combined = query + "\n\n" + rag_context + "\n\n" + web_context
29
  inputs = tokenizer(combined, return_tensors="pt", truncation=True, padding="max_length", max_length=128)
30
  input_ids = inputs["input_ids"].to(device)
31
 
32
- # Step 4: Evo prediction
33
  with torch.no_grad():
34
  logits = evo_model(input_ids)
35
  pred = int(torch.sigmoid(logits).item() > 0.5)
36
 
37
- return f"Evo suggests: Option {pred + 1}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # --- GPT-3.5 Inference (OpenAI >= 1.0.0) ---
40
- openai_api_key = os.environ.get("OPENAI_API_KEY", "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") # Replace or use HF secret
41
  client = OpenAI(api_key=openai_api_key)
42
 
43
  def get_gpt_response(query, context):
 
16
  # --- Load Tokenizer ---
17
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
18
 
19
+ # --- EvoRAG+ with Rich Reasoning ---
20
  def evo_rag_response(query):
21
+ # Step 1: Get context from RAG (doc) + web
22
  rag_context = retrieve(query)
 
 
23
  web_context = web_search(query)
24
 
25
+ # Step 2: Combine for inference
26
  combined = query + "\n\n" + rag_context + "\n\n" + web_context
27
  inputs = tokenizer(combined, return_tensors="pt", truncation=True, padding="max_length", max_length=128)
28
  input_ids = inputs["input_ids"].to(device)
29
 
30
+ # Step 3: Evo decision
31
  with torch.no_grad():
32
  logits = evo_model(input_ids)
33
  pred = int(torch.sigmoid(logits).item() > 0.5)
34
 
35
+ # Step 4: Extract Option Texts if available
36
+ option_text = ""
37
+ if "Option 1:" in query and "Option 2:" in query:
38
+ try:
39
+ opt1 = query.split("Option 1:")[1].split("Option 2:")[0].strip()
40
+ opt2 = query.split("Option 2:")[1].strip()
41
+ option_text = opt1 if pred == 0 else opt2
42
+ except:
43
+ pass
44
+
45
+ # Step 5: Format output
46
+ output = f"🧠 Evo suggests: Option {pred + 1}"
47
+ if option_text:
48
+ output += f"\n➡️ {option_text}"
49
+
50
+ output += "\n\n📌 Reasoning:\n"
51
+ if rag_context:
52
+ first_line = rag_context.strip().splitlines()[0][:250]
53
+ output += f"- {first_line}...\n"
54
+ else:
55
+ output += "- No document insight available.\n"
56
+
57
+ output += "\n📂 Context used:\n" + (rag_context[:400] if rag_context else "[None]")
58
+
59
+ output += "\n\n🌐 Web insight:\n" + (web_context[:400] if web_context else "[None]")
60
+
61
+ return output
62
 
63
+ # --- GPT-3.5 (OpenAI >= 1.0.0) ---
64
+ openai_api_key = os.environ.get("OPENAI_API_KEY", "sk-...") # Replace or set via HF secrets
65
  client = OpenAI(api_key=openai_api_key)
66
 
67
  def get_gpt_response(query, context):