Talha812 commited on
Commit
4b72582
·
verified ·
1 Parent(s): 60fa5f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 langchain.embeddings import HuggingFaceEmbeddings
8
+ from langchain.vectorstores import FAISS
9
+ import gradio as gr
10
+
11
+ # Get GROQ_API_KEY from env
12
+ groq_api_key = os.environ.get("GROQ_API_KEY")
13
+ groq_client = Groq(api_key=groq_api_key)
14
+
15
+ # Load and embed documents
16
+ def load_documents_and_create_vectorstore():
17
+ docs = []
18
+ for file in ["documents/ASTM1.pdf", "documents/ASTM2.pdf"]:
19
+ loader = PyPDFLoader(file)
20
+ docs.extend(loader.load())
21
+
22
+ splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
23
+ chunks = splitter.split_documents(docs)
24
+
25
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
26
+ vectorstore = FAISS.from_documents(chunks, embeddings)
27
+
28
+ return vectorstore
29
+
30
+ vectorstore = load_documents_and_create_vectorstore()
31
+
32
+ # RAG: Ask question using context + Groq
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
+ prompt = f"""You are a helpful Civil Engineering assistant. Use the ASTM standard context below to answer:
40
+
41
+ Context:
42
+ {context}
43
+
44
+ Question: {question}
45
+ Answer:"""
46
+
47
+ completion = groq_client.chat.completions.create(
48
+ messages=[{"role": "user", "content": prompt}],
49
+ model="llama-3.3-70b-versatile"
50
+ )
51
+
52
+ return completion.choices[0].message.content
53
+
54
+ # Gradio UI
55
+ gr.Interface(
56
+ fn=ask_question,
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 any question about ASTM Civil Engineering Standards"
61
+ ).launch()