File size: 3,343 Bytes
09a9add
 
 
 
 
4b268d1
09a9add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b268d1
09a9add
 
 
 
 
 
 
 
4b268d1
09a9add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b268d1
 
 
 
 
 
 
 
09a9add
 
4b268d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09a9add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b268d1
 
 
 
 
 
 
09a9add
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from fastapi import FastAPI, UploadFile, File
import pandas as pd
import faiss
import pickle
import os
import json
from sentence_transformers import SentenceTransformer
from exa_py import Exa
from groq import Groq

app = FastAPI()

# =============================
# πŸ”‘ KEYS
# =============================
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
EXA_API_KEY = os.getenv("EXA_API_KEY")

client = Groq(api_key=GROQ_API_KEY)
exa = Exa(api_key=EXA_API_KEY)

# =============================
# 🧠 LOAD MODELS
# =============================
embed_model = SentenceTransformer('all-mpnet-base-v2')

index = faiss.read_index("faiss_index.index")

with open("startup_texts.pkl", "rb") as f:
    startup_texts = pickle.load(f)

# =============================
# πŸ” RETRIEVAL
# =============================
def retrieve_similar(problem, k=3):
    vec = embed_model.encode([problem], convert_to_numpy=True)
    distances, indices = index.search(vec, k)

    return [
        {"text": startup_texts[idx], "score": float(distances[0][i])}
        for i, idx in enumerate(indices[0])
    ]

# =============================
# 🌐 WEB SEARCH
# =============================
def search_web(query):
    try:
        response = exa.search(query, num_results=5)
        return [r.text or r.summary or "" for r in response.results]
    except:
        return []

# =============================
# πŸ€– QWEN (Groq)
# =============================
def ask_qwen(prompt):
    completion = client.chat.completions.create(
        model="qwen/qwen3-32b",
        messages=[
            {"role": "system", "content": "You are a strict fact-checking analyst. Return ONLY valid JSON."},
            {"role": "user", "content": prompt}
        ],
        temperature=0.3,
        max_tokens=512
    )
    return completion.choices[0].message.content

# =============================
# 🧠 PIPELINE (FIXED)
# =============================
def analyze_problem(problem):

    retrieved = retrieve_similar(problem)

    if retrieved and retrieved[0]["score"] < 2.0:
        context = "\n\n".join([r["text"] for r in retrieved])
    else:
        web = search_web(problem)
        context = "\n\n".join(web[:3])

    prompt = f"""
Problem:
{problem}

Evidence:
{context}

Return ONLY valid JSON in this format:

{{
  "status": "SOLVED or UNSOLVED",
  "reason": "one short sentence",
  "gaps": ["gap1", "gap2"],
  "new_problem": "rewrite of the problem"
}}
"""

    response = ask_qwen(prompt)

    try:
        parsed = json.loads(response)
    except:
        parsed = {
            "status": "ERROR",
            "reason": "invalid JSON from model",
            "gaps": [],
            "new_problem": ""
        }

    parsed["problem"] = problem

    return parsed

# =============================
# πŸš€ API
# =============================
@app.post("/analyze")
async def analyze(file: UploadFile = File(...)):

    df = pd.read_csv(file.file)

    results = []

    for p in df.iloc[:, 0].tolist():
        try:
            results.append(analyze_problem(p))
        except Exception as e:
            results.append({
                "problem": p,
                "status": "ERROR",
                "reason": str(e),
                "gaps": [],
                "new_problem": ""
            })

    return {
        "success": True,
        "results": results
    }