Spaces:
Sleeping
Sleeping
Update app.py
Browse files12312 unmodified lines16171819202157 unmodified lines7980818283848586878889909192import osimport reimport numpy as np12 unmodified linesembedder = SentenceTransformer(EMBED_ID)client = Groq(api_key=os.environ.get("GROQ_API_KEY"))SYSTEM_PROMPT = """You are a strict teaching assistant for ONE specific course.You will receive: (1) the course LEVEL description, (2) EXCERPTS from the coursematerial uploaded by the teacher/student, and (3) an ASSIGNMENT.57 unmodified linessources.append(name)if not chunks:return None, "⚠️ لم أستطع استخراج نص من الملفات (قد تكون صوراً ممسوحة ضوئياً)."vecs = embedder.encode(chunks, normalize_embeddings=True)index = {"chunks": chunks, "sources": sources, "vecs": vecs}return index, f"✅ تمت قراءة {len(files)} ملف(ات) وتقسيمها إلى {len(chunks)} مقطع."# ---------- البحث ----------def retrieve(index, query: str, k: int):q = embedder.encode([query], normalize_embeddings=True)[0]scores = index["vecs"] @ qtop = np.argsort(-scores)[:k]return [(int(i), float(scores[i])) for i in top]123456789101112 unmodified lines242526272829303132333457 unmodified lines9293949596979899100101102103104105# يجب استيراد spaces قبل أي مكتبة أخرى (مطلوب لعتاد ZeroGPU)try:import spacesgpu = spaces.GPUexcept ImportError: # على CPU العاديdef gpu(f):return fimport osimport reimport numpy as np12 unmodified linesembedder = SentenceTransformer(EMBED_ID)client = Groq(api_key=os.environ.get("GROQ_API_KEY"))@gpudef encode(texts):return embedder.encode(texts, normalize_embeddings=True)SYSTEM_PROMPT = """You are a strict teaching assistant for ONE specific course.You will receive: (1) the course LEVEL description, (2) EXCERPTS from the coursematerial uploaded by the teacher/student, and (3) an ASSIGNMENT.57 unmodified linessources.append(name)if not chunks:return None, "⚠️ لم أستطع استخراج نص من الملفات (قد تكون صوراً ممسوحة ضوئياً)."vecs = encode(chunks)index = {"chunks": chunks, "sources": sources, "vecs": vecs}return index, f"✅ تمت قراءة {len(files)} ملف(ات) وتقسيمها إلى {len(chunks)} مقطع."# ---------- البحث ----------def retrieve(index, query: str, k: int):q = encode([query])[0]scores = index["vecs"] @ qtop = np.argsort(-scores)[:k]return [(int(i), float(scores[i])) for i in top]
app.py+15-2