Spaces:
Sleeping
Sleeping
Update app.py
Browse files74 unmodified lines757677787980818283848586878889909192939495969798991001011022 unmodified lines1051061071081091101111121131141151161171181191201211221231241251261271281292 unmodified lines13213313413513613774 unmodified linesif not model or not index or not document_ids or not documents or not llm_pipeline:return "System not fully initialized. Please check logs for missing components."# 1. Query Embeddingquery_embedding = model.encode([query])# 2. Retrieval using FAISSD, I = index.search(query_embedding, top_k)retrieved_docs = []for i in I[0]:sol_id = document_ids[i]# Find the full content of the retrieved SOL# --- CHANGE THIS LINE ---# Original (incorrect): retrieved_content = next((doc["text"] for doc in documents if doc["id"] == sol_id), "Content not found.")retrieved_content = next((doc["content"] for doc in documents if doc["id"] == sol_id), "Content not found.")retrieved_docs.append({"id": sol_id, "content": retrieved_content})# 3. Context Construction# --- CHANGE THIS LINE ---# Original (incorrect): context = "\n\n".join([f"SOL {doc['id']}: {doc['text']}" for doc in retrieved_docs])context = "\n\n".join([f"SOL {doc['id']}: {doc['content']}" for doc in retrieved_docs])# 4. LLM Generationprompt = f"""Given the following information about Virginia Standards of Learning (SOLs):{context}Based on this information, answer the following question:2 unmodified linesIf asked for lesson plans, worksheets, or proofs, explain what the document generally entails and whether it provides such materials.Be concise and to the point."""print(f"\n--- PROMPT SENT TO LLM ---\n{prompt}\n--------------------------\n")response = llm_pipeline(prompt, max_new_tokens=500, num_return_sequences=1, do_sample=True, temperature=0.7)generated_text = response[0]['generated_text']print(f"\n--- RAW GENERATED TEXT ---\n{generated_text}\n--------------------------\n")answer_start_marker = f"Based on this information, answer the following question:\n{query}"if answer_start_marker in generated_text:answer = generated_text.split(answer_start_marker, 1)[1].strip()answer = re.sub(r'If the question is about a specific SOL number,.*?$', '', answer, flags=re.DOTALL).strip()else:answer = generated_textprint(f"\n--- FINAL ANSWER ---\n{answer}\n--------------------\n")return answer# Create Gradio interfacedemo = gr.Interface(fn=retrieve_and_generate_app,inputs=gr.Textbox(lines=2, placeholder="Enter your geometry-related question here..."),2 unmodified linesdescription="Ask questions about the Geometry SOL Instructional Guide")# Launch the appif __name__ == "__main__":demo.launch()74 unmodified lines7576777879808182838485868788899091929394959697982 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251261271282 unmodified lines13113213313413574 unmodified linesif not model or not index or not document_ids or not documents or not llm_pipeline:return "System not fully initialized. Please check logs for missing components."try:# 1. Query Embeddingquery_embedding = model.encode([query])# 2. Retrieval using FAISSD, I = index.search(query_embedding, top_k)retrieved_docs = []for i in I[0]:sol_id = document_ids[i]retrieved_content = next((doc["content"] for doc in documents if doc["id"] == sol_id), "Content not found.")retrieved_docs.append({"id": sol_id, "content": retrieved_content})# 3. Context Constructioncontext = "\n\n".join([f"SOL {doc['id']}: {doc['content']}" for doc in retrieved_docs])# 4. LLM Generationprompt = f"""Given the following information about Virginia Standards of Learning (SOLs):{context}Based on this information, answer the following question:2 unmodified linesIf asked for lesson plans, worksheets, or proofs, explain what the document generally entails and whether it provides such materials.Be concise and to the point."""print(f"\n--- PROMPT SENT TO LLM ---\n{prompt}\n--------------------------\n")response = llm_pipeline(prompt, max_new_tokens=500, num_return_sequences=1, do_sample=True, temperature=0.7)generated_text = response[0]['generated_text']print(f"\n--- RAW GENERATED TEXT ---\n{generated_text}\n--------------------------\n")answer_start_marker = f"Based on this information, answer the following question:\n{query}"if answer_start_marker in generated_text:answer = generated_text.split(answer_start_marker, 1)[1].strip()answer = re.sub(r'If the question is about a specific SOL number,.*?$', '', answer, flags=re.DOTALL).strip()else:answer = generated_textprint(f"\n--- FINAL ANSWER ---\n{answer}\n--------------------\n")return answer if answer else "No valid response generated. Check logs for details."except Exception as e:print(f"\n--- ERROR ---\n{str(e)}\n------------\n")return f"An error occurred: {str(e)}. Please check the logs for more details."# Create Gradio interfacedemo = gr.Interface(fn=retrieve_and_generate_app,inputs=gr.Textbox(lines=2, placeholder="Enter your geometry-related question here..."),2 unmodified linesdescription="Ask questions about the Geometry SOL Instructional Guide")if __name__ == "__main__":demo.launch()
app.py+34-36