Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135# app.pyimport gradio as grimport osfrom transformers import pipelinefrom sentence_transformers import SentenceTransformerimport faissimport numpy as npimport jsonimport re# --- Load necessary components for the RAG system ---# These paths are relative to the Space's root directoryFAISS_INDEX_PATH = "sol_faiss_index.bin"DOCUMENT_IDS_PATH = "sol_document_ids.json"# Load SentenceTransformer model# Ensure this model is downloaded or available in the environment# For Spaces, you might need to add it to requirements.txt or directly download if space has internet# It's better to declare it globally or as a shared resource.try:model = SentenceTransformer('all-mpnet-base-v2')except Exception as e:print(f"Error loading SentenceTransformer model: {e}")print("Attempting to load from local cache or download on first use.")# If running in a Space, the model will be downloaded to cache if not present.# Ensure you have internet access in your Space settings.# Load FAISS indextry:index = faiss.read_index(FAISS_INDEX_PATH)except Exception as e:print(f"Error loading FAISS index: {e}")# Handle error, maybe create a dummy index or exitindex = None # Placeholder if loading fails# Load document IDstry:with open(DOCUMENT_IDS_PATH, "r") as f:document_ids = json.load(f)except Exception as e:print(f"Error loading document IDs: {e}")document_ids = [] # Placeholder if loading fails# Placeholder for the actual content of "10 Geometry Mathematics Instructional Guide.pdf"# In a real deployed scenario, this content would be loaded from a file# that you upload to your Hugging Face Space or fetched at runtime.# For now, we'll assume it's available or that 'documents' are pre-processed and loaded.# You would typically load the 'documents' list created in Step 2 here.# For deployment, it's best to save the `documents` list (sol_data) as a JSON# and load it back. Let's add that.# Assuming you've saved sol_data as 'sol_documents.json'SOL_DOCUMENTS_PATH = "sol_documents.json"try:with open(SOL_DOCUMENTS_PATH, "r") as f:documents = json.load(f)except Exception as e:print(f"Error loading sol documents: {e}")documents = [] # Placeholder# Load LLM for generation# For a Hugging Face Space, you need to ensure the model is available.# 'google/gemma-2b-it' is a good option.# Ensure you set up environment variables or secrets for API keys if using paid models.try:# llm_pipeline = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")llm_pipeline = pipeline("text-generation", model="google/gemma-2b-it")except Exception as e:print(f"Error loading LLM pipeline: {e}")llm_pipeline = None # Placeholderdef retrieve_and_generate_app(query, top_k=3):if 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:{query}If the question is about a specific SOL number, provide a direct explanation for that SOL.If 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..."),outputs=gr.Textbox(label="Generated Answer"),title="Virginia SOL Geometry Assistant",description="Ask questions about the Geometry SOL Instructional Guide")if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import osimport reimport gradio as grfrom langchain.prompts import PromptTemplatefrom langchain_openai import ChatOpenAIfrom langchain.vectorstores import Chromafrom langchain.embeddings import HuggingFaceEmbeddings# Load embedding model and vector store from persisted DBembedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")vector_store = Chroma(embedding=embedding_model,persist_directory="geometry_db", # relative folder inside your Hugging Face Spacecollection_name="geometry_sol")# Load OpenAI key (you must add this in Hugging Face Space Secrets)os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")# Load the LLM (GPT-3.5)llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3)# Prompt templatestemplates = {"general": PromptTemplate(input_variables=["context", "query"],template="""You are a strict assistant for the Virginia Geometry SOL.Only use exact phrases from the following SOL text:{context}Answer the question: "{query}"If the answer is in the SOL text, quote it exactly. Do not rephrase or summarize. Do not add your own explanation.If the answer is not in the context, reply: "The answer is not found in the provided SOL text.""""),"lesson plan": PromptTemplate(input_variables=["context", "query"],template="""Given the following retrieved SOL text:{context}Generate a Geometry lesson plan based on: "{query}"Include:1. Simple explanation of the concept.2. Real-world example.3. Engaging class activity.Be concise and curriculum-aligned for high school."""),"worksheet": PromptTemplate(input_variables=["context", "query"],template="""{context}Create a student worksheet for: "{query}"Include: concept summary, a worked example, and 3 practice problems."""),"proofs": PromptTemplate(input_variables=["context", "query"],template="""{context}Generate a proof-focused geometry lesson plan for: "{query}"Include: student-friendly explanation, real-world link, and activity.""")}# Optional: shortcut to solve simple math problems (like area of rectangle)def try_math_solver(query):match = re.search(r"rectangle.*l\s*=\s*(\d+).+w\s*=\s*(\d+)", query.lower())if match:l, w = int(match.group(1)), int(match.group(2))return f"The area of the rectangle is {l} × {w} = {l * w} square units."return None# RAG functiondef rag_query(query, mode="general"):docs = vector_store.similarity_search(query, k=2)context = "\n\n".join([doc.page_content for doc in docs])prompt = templates[mode].format_prompt(context=context, query=query).to_string()return llm.invoke(prompt).content# Gradio app functiondef ask_geometry_sol(query, mode):math_result = try_math_solver(query)if math_result:return math_resulttry:return rag_query(query, mode)except Exception as e:return f"⚠️ Error: {type(e).__name__} - {str(e)}"# Gradio UIiface = gr.Interface(fn=ask_geometry_sol,inputs=[gr.Textbox(label="Enter your Geometry SOL question or topic"),gr.Radio(["general", "lesson plan", "worksheet", "proofs"], value="general", label="Response type")],outputs="text",title="📘 Virginia Geometry SOL Assistant",description="Ask about any 2023 Geometry SOL (Standards of Learning). Get exact quotes, lesson plans, worksheets, or proof-based lessons.")if __name__ == "__main__":iface.launch()
app.py+95-118