Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
from groq import Groq
|
| 5 |
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
groq_client = Groq(api_key=groq_api_key)
|
| 14 |
|
| 15 |
-
# Load and
|
| 16 |
def load_documents_and_create_vectorstore():
|
| 17 |
docs = []
|
| 18 |
for file in ["documents/ASTM1.pdf", "documents/ASTM2.pdf"]:
|
|
@@ -27,22 +24,22 @@ def load_documents_and_create_vectorstore():
|
|
| 27 |
|
| 28 |
return vectorstore
|
| 29 |
|
|
|
|
| 30 |
vectorstore = load_documents_and_create_vectorstore()
|
| 31 |
|
| 32 |
-
# RAG
|
| 33 |
def ask_question(question):
|
| 34 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 35 |
docs = retriever.get_relevant_documents(question)
|
| 36 |
|
| 37 |
context = "\n".join([doc.page_content for doc in docs])
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
Context:
|
| 42 |
-
{context}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
completion = groq_client.chat.completions.create(
|
| 48 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -57,5 +54,5 @@ gr.Interface(
|
|
| 57 |
inputs=gr.Textbox(label="Ask a Civil Engineering Question (based on ASTM)"),
|
| 58 |
outputs=gr.Textbox(label="Answer"),
|
| 59 |
title="Civil Engineering RAG Assistant",
|
| 60 |
-
description="Ask
|
| 61 |
).launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from groq import Groq
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Initialize Groq
|
| 10 |
+
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
|
|
| 11 |
|
| 12 |
+
# Load documents and create vector DB
|
| 13 |
def load_documents_and_create_vectorstore():
|
| 14 |
docs = []
|
| 15 |
for file in ["documents/ASTM1.pdf", "documents/ASTM2.pdf"]:
|
|
|
|
| 24 |
|
| 25 |
return vectorstore
|
| 26 |
|
| 27 |
+
# Load documents on startup
|
| 28 |
vectorstore = load_documents_and_create_vectorstore()
|
| 29 |
|
| 30 |
+
# RAG question answering function
|
| 31 |
def ask_question(question):
|
| 32 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 33 |
docs = retriever.get_relevant_documents(question)
|
| 34 |
|
| 35 |
context = "\n".join([doc.page_content for doc in docs])
|
| 36 |
+
prompt = f"""You are a helpful Civil Engineering assistant. Use the following ASTM standard context to answer:
|
| 37 |
|
| 38 |
+
Context:
|
| 39 |
+
{context}
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
Question: {question}
|
| 42 |
+
Answer:"""
|
| 43 |
|
| 44 |
completion = groq_client.chat.completions.create(
|
| 45 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 54 |
inputs=gr.Textbox(label="Ask a Civil Engineering Question (based on ASTM)"),
|
| 55 |
outputs=gr.Textbox(label="Answer"),
|
| 56 |
title="Civil Engineering RAG Assistant",
|
| 57 |
+
description="Ask questions from uploaded ASTM PDFs"
|
| 58 |
).launch()
|