Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
from langchain.document_loaders import PyPDFLoader
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
from langchain.llms import HuggingFacePipeline
|
9 |
+
from transformers import pipeline
|
10 |
+
from groq import Groq
|
11 |
+
import requests
|
12 |
+
from PyPDF2 import PdfReader
|
13 |
+
import io
|
14 |
+
|
15 |
+
# Set up API key for Groq API
|
16 |
+
GROQ_API_KEY = "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"
|
17 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
18 |
+
|
19 |
+
# Initialize Groq API client
|
20 |
+
client = Groq(api_key=GROQ_API_KEY)
|
21 |
+
|
22 |
+
# Predefined PDF link
|
23 |
+
pdf_url = "https://drive.google.com/file/d/1P9InkDWyaybb8jR_xS4f4KsxTlYip8RA/view?usp=drive_link"
|
24 |
+
|
25 |
+
def extract_text_from_pdf(pdf_url):
|
26 |
+
"""Extract text from a PDF file given its Google Drive shared link."""
|
27 |
+
# Extract file ID from the Google Drive link
|
28 |
+
file_id = pdf_url.split('/d/')[1].split('/view')[0]
|
29 |
+
download_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
30 |
+
response = requests.get(download_url)
|
31 |
+
|
32 |
+
if response.status_code == 200:
|
33 |
+
pdf_content = io.BytesIO(response.content)
|
34 |
+
reader = PdfReader(pdf_content)
|
35 |
+
text = "\n".join([page.extract_text() for page in reader.pages])
|
36 |
+
return text
|
37 |
+
else:
|
38 |
+
st.error("Failed to download PDF.")
|
39 |
+
return ""
|
40 |
+
|
41 |
+
# Streamlit Interface
|
42 |
+
st.title("ASD Diagnosis Retrieval-Augmented Generation App")
|
43 |
+
|
44 |
+
st.info("Processing predefined PDF...")
|
45 |
+
extracted_text = extract_text_from_pdf(pdf_url)
|
46 |
+
|
47 |
+
if extracted_text:
|
48 |
+
st.success("Text extraction complete.")
|
49 |
+
|
50 |
+
# Preprocess text for embeddings
|
51 |
+
st.info("Generating embeddings...")
|
52 |
+
embeddings_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
53 |
+
embeddings = embeddings_model.embed_documents([extracted_text])
|
54 |
+
|
55 |
+
# Store embeddings in FAISS
|
56 |
+
st.info("Storing embeddings in FAISS...")
|
57 |
+
faiss_index = FAISS.from_texts([extracted_text], embeddings_model)
|
58 |
+
|
59 |
+
# Set up Hugging Face LLM pipeline
|
60 |
+
st.info("Setting up RAG pipeline...")
|
61 |
+
hf_pipeline = pipeline("text-generation", model="google/flan-t5-base", tokenizer="google/flan-t5-base")
|
62 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
63 |
+
|
64 |
+
retriever = faiss_index.as_retriever()
|
65 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
66 |
+
|
67 |
+
# Query interface
|
68 |
+
st.success("RAG pipeline ready.")
|
69 |
+
user_query = st.text_input("Enter your query about ASD:")
|
70 |
+
|
71 |
+
if user_query:
|
72 |
+
st.info("Fetching response...")
|
73 |
+
response = qa_chain.run(user_query)
|
74 |
+
st.success(response)
|
75 |
+
else:
|
76 |
+
st.error("No text extracted from the PDF.")
|