Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import faiss
|
| 4 |
import pickle
|
| 5 |
import os
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from exa_py import Exa
|
| 8 |
from groq import Groq
|
|
@@ -15,7 +16,6 @@ app = FastAPI()
|
|
| 15 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 16 |
EXA_API_KEY = os.getenv("EXA_API_KEY")
|
| 17 |
|
| 18 |
-
# clients
|
| 19 |
client = Groq(api_key=GROQ_API_KEY)
|
| 20 |
exa = Exa(api_key=EXA_API_KEY)
|
| 21 |
|
|
@@ -58,7 +58,7 @@ def ask_qwen(prompt):
|
|
| 58 |
completion = client.chat.completions.create(
|
| 59 |
model="qwen/qwen3-32b",
|
| 60 |
messages=[
|
| 61 |
-
{"role": "system", "content": "You are a strict fact-checking analyst."},
|
| 62 |
{"role": "user", "content": prompt}
|
| 63 |
],
|
| 64 |
temperature=0.3,
|
|
@@ -67,7 +67,7 @@ def ask_qwen(prompt):
|
|
| 67 |
return completion.choices[0].message.content
|
| 68 |
|
| 69 |
# =============================
|
| 70 |
-
# 🧠 PIPELINE
|
| 71 |
# =============================
|
| 72 |
def analyze_problem(problem):
|
| 73 |
|
|
@@ -86,16 +86,31 @@ Problem:
|
|
| 86 |
Evidence:
|
| 87 |
{context}
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
rewrite
|
|
|
|
| 96 |
"""
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# =============================
|
| 101 |
# 🚀 API
|
|
@@ -111,7 +126,13 @@ async def analyze(file: UploadFile = File(...)):
|
|
| 111 |
try:
|
| 112 |
results.append(analyze_problem(p))
|
| 113 |
except Exception as e:
|
| 114 |
-
results.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
return {
|
| 117 |
"success": True,
|
|
|
|
| 3 |
import faiss
|
| 4 |
import pickle
|
| 5 |
import os
|
| 6 |
+
import json
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from exa_py import Exa
|
| 9 |
from groq import Groq
|
|
|
|
| 16 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 17 |
EXA_API_KEY = os.getenv("EXA_API_KEY")
|
| 18 |
|
|
|
|
| 19 |
client = Groq(api_key=GROQ_API_KEY)
|
| 20 |
exa = Exa(api_key=EXA_API_KEY)
|
| 21 |
|
|
|
|
| 58 |
completion = client.chat.completions.create(
|
| 59 |
model="qwen/qwen3-32b",
|
| 60 |
messages=[
|
| 61 |
+
{"role": "system", "content": "You are a strict fact-checking analyst. Return ONLY valid JSON."},
|
| 62 |
{"role": "user", "content": prompt}
|
| 63 |
],
|
| 64 |
temperature=0.3,
|
|
|
|
| 67 |
return completion.choices[0].message.content
|
| 68 |
|
| 69 |
# =============================
|
| 70 |
+
# 🧠 PIPELINE (FIXED)
|
| 71 |
# =============================
|
| 72 |
def analyze_problem(problem):
|
| 73 |
|
|
|
|
| 86 |
Evidence:
|
| 87 |
{context}
|
| 88 |
|
| 89 |
+
Return ONLY valid JSON in this format:
|
| 90 |
+
|
| 91 |
+
{{
|
| 92 |
+
"status": "SOLVED or UNSOLVED",
|
| 93 |
+
"reason": "one short sentence",
|
| 94 |
+
"gaps": ["gap1", "gap2"],
|
| 95 |
+
"new_problem": "rewrite of the problem"
|
| 96 |
+
}}
|
| 97 |
"""
|
| 98 |
|
| 99 |
+
response = ask_qwen(prompt)
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
parsed = json.loads(response)
|
| 103 |
+
except:
|
| 104 |
+
parsed = {
|
| 105 |
+
"status": "ERROR",
|
| 106 |
+
"reason": "invalid JSON from model",
|
| 107 |
+
"gaps": [],
|
| 108 |
+
"new_problem": ""
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
parsed["problem"] = problem
|
| 112 |
+
|
| 113 |
+
return parsed
|
| 114 |
|
| 115 |
# =============================
|
| 116 |
# 🚀 API
|
|
|
|
| 126 |
try:
|
| 127 |
results.append(analyze_problem(p))
|
| 128 |
except Exception as e:
|
| 129 |
+
results.append({
|
| 130 |
+
"problem": p,
|
| 131 |
+
"status": "ERROR",
|
| 132 |
+
"reason": str(e),
|
| 133 |
+
"gaps": [],
|
| 134 |
+
"new_problem": ""
|
| 135 |
+
})
|
| 136 |
|
| 137 |
return {
|
| 138 |
"success": True,
|