Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import tempfile
|
| 3 |
import logging
|
| 4 |
-
import time
|
| 5 |
from typing import List
|
|
|
|
| 6 |
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
-
from
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
from langchain.chains.summarize import load_summarize_chain
|
|
@@ -19,12 +19,13 @@ logger = logging.getLogger(__name__)
|
|
| 19 |
|
| 20 |
# Constants
|
| 21 |
EMBEDDING_MODEL = 'sentence-transformers/all-MiniLM-L6-v2'
|
| 22 |
-
DEFAULT_MODEL = "
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
@st.cache_resource
|
| 28 |
def load_embeddings():
|
| 29 |
"""Load and cache the embedding model."""
|
| 30 |
try:
|
|
@@ -34,15 +35,15 @@ def load_embeddings():
|
|
| 34 |
st.error("Failed to load the embedding model. Please try again later.")
|
| 35 |
return None
|
| 36 |
|
| 37 |
-
@st.cache_resource
|
| 38 |
def load_llm(model_name):
|
| 39 |
"""Load and cache the language model."""
|
| 40 |
try:
|
| 41 |
-
pipe = pipeline("text-generation", model=model_name, max_length=1024)
|
| 42 |
return HuggingFacePipeline(pipeline=pipe)
|
| 43 |
except Exception as e:
|
| 44 |
logger.error(f"Failed to load LLM: {e}")
|
| 45 |
-
st.error(f"Failed to load the model {model_name}. Please try
|
| 46 |
return None
|
| 47 |
|
| 48 |
def process_pdf(file) -> List[Document]:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tempfile
|
| 3 |
import logging
|
|
|
|
| 4 |
from typing import List
|
| 5 |
+
import torch
|
| 6 |
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_community.llms import HuggingFacePipeline
|
| 10 |
from langchain.chains.summarize import load_summarize_chain
|
|
|
|
| 19 |
|
| 20 |
# Constants
|
| 21 |
EMBEDDING_MODEL = 'sentence-transformers/all-MiniLM-L6-v2'
|
| 22 |
+
DEFAULT_MODEL = "microsoft/phi-2"
|
| 23 |
|
| 24 |
+
# Check for GPU
|
| 25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
+
print(f"Using device: {device}")
|
| 27 |
|
| 28 |
+
@st.cache_resource
|
| 29 |
def load_embeddings():
|
| 30 |
"""Load and cache the embedding model."""
|
| 31 |
try:
|
|
|
|
| 35 |
st.error("Failed to load the embedding model. Please try again later.")
|
| 36 |
return None
|
| 37 |
|
| 38 |
+
@st.cache_resource
|
| 39 |
def load_llm(model_name):
|
| 40 |
"""Load and cache the language model."""
|
| 41 |
try:
|
| 42 |
+
pipe = pipeline("text-generation", model=model_name, device=device, max_length=1024)
|
| 43 |
return HuggingFacePipeline(pipeline=pipe)
|
| 44 |
except Exception as e:
|
| 45 |
logger.error(f"Failed to load LLM: {e}")
|
| 46 |
+
st.error(f"Failed to load the model {model_name}. Please try another model or check your internet connection.")
|
| 47 |
return None
|
| 48 |
|
| 49 |
def process_pdf(file) -> List[Document]:
|