Spaces:
Runtime error
Runtime error
Update gradio_app.py
Browse files- gradio_app.py +78 -61
gradio_app.py
CHANGED
@@ -1,27 +1,22 @@
|
|
1 |
import gradio as gr
|
2 |
-
from llama_index.core import
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
from llama_index.core import Settings
|
7 |
import os
|
8 |
import tempfile
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
# Configure the Llama index settings
|
14 |
-
Settings.llm = HuggingFaceInferenceAPI(
|
15 |
-
model_name="google/gemma-1.1-7b-it",
|
16 |
-
tokenizer_name="google/gemma-1.1-7b-it",
|
17 |
-
context_window=3000,
|
18 |
-
token=os.getenv("HF_TOKEN"),
|
19 |
-
max_new_tokens=512,
|
20 |
-
generate_kwargs={"temperature": 0.1},
|
21 |
-
)
|
22 |
-
Settings.embed_model = HuggingFaceEmbedding(
|
23 |
-
model_name="BAAI/bge-small-en-v1.5"
|
24 |
-
)
|
25 |
|
26 |
# Define the directory for persistent storage and data
|
27 |
PERSIST_DIR = "./db"
|
@@ -31,57 +26,79 @@ DATA_DIR = "data"
|
|
31 |
os.makedirs(DATA_DIR, exist_ok=True)
|
32 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
34 |
def data_ingestion():
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def handle_query(query):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
|
66 |
def process_file(file):
|
67 |
if file is None:
|
68 |
return "Please upload a PDF file."
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
def chat_function(message, history):
|
87 |
response = handle_query(message)
|
|
|
1 |
import gradio as gr
|
2 |
+
from llama_index.core import (
|
3 |
+
StorageContext,
|
4 |
+
load_index_from_storage,
|
5 |
+
VectorStoreIndex,
|
6 |
+
SimpleDirectoryReader,
|
7 |
+
ChatPromptTemplate,
|
8 |
+
)
|
9 |
+
from llama_index.llms.huggingface import HuggingFaceInferencePipeline
|
10 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
11 |
from llama_index.core import Settings
|
12 |
import os
|
13 |
import tempfile
|
14 |
+
from pathlib import Path
|
15 |
+
import logging
|
16 |
|
17 |
+
# Set up logging
|
18 |
+
logging.basicConfig(level=logging.INFO)
|
19 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Define the directory for persistent storage and data
|
22 |
PERSIST_DIR = "./db"
|
|
|
26 |
os.makedirs(DATA_DIR, exist_ok=True)
|
27 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
28 |
|
29 |
+
# Configure the Llama index settings
|
30 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
31 |
+
model_name="BAAI/bge-small-en-v1.5"
|
32 |
+
)
|
33 |
+
|
34 |
def data_ingestion():
|
35 |
+
try:
|
36 |
+
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
37 |
+
if not documents:
|
38 |
+
logger.warning("No documents loaded from the data directory.")
|
39 |
+
return False
|
40 |
+
storage_context = StorageContext.from_defaults()
|
41 |
+
index = VectorStoreIndex.from_documents(documents)
|
42 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
43 |
+
return True
|
44 |
+
except Exception as e:
|
45 |
+
logger.error(f"Error during data ingestion: {str(e)}")
|
46 |
+
return False
|
47 |
|
48 |
def handle_query(query):
|
49 |
+
try:
|
50 |
+
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
51 |
+
index = load_index_from_storage(storage_context)
|
52 |
+
chat_text_qa_msgs = [
|
53 |
+
(
|
54 |
+
"user",
|
55 |
+
"""You are a Q&A assistant named EazyPeazy. Your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
|
56 |
+
Context:
|
57 |
+
{context_str}
|
58 |
+
Question:
|
59 |
+
{query_str}
|
60 |
+
"""
|
61 |
+
)
|
62 |
+
]
|
63 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
64 |
+
|
65 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
66 |
+
answer = query_engine.query(query)
|
67 |
+
|
68 |
+
if hasattr(answer, 'response'):
|
69 |
+
return answer.response
|
70 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
71 |
+
return answer['response']
|
72 |
+
else:
|
73 |
+
return "Sorry, I couldn't find an answer."
|
74 |
+
except Exception as e:
|
75 |
+
logger.error(f"Error handling query: {str(e)}")
|
76 |
+
return "An error occurred while processing your query. Please try again."
|
77 |
|
78 |
def process_file(file):
|
79 |
if file is None:
|
80 |
return "Please upload a PDF file."
|
81 |
|
82 |
+
try:
|
83 |
+
temp_dir = tempfile.mkdtemp()
|
84 |
+
temp_path = Path(temp_dir) / "uploaded.pdf"
|
85 |
+
|
86 |
+
with open(temp_path, "wb") as f:
|
87 |
+
f.write(file.read())
|
88 |
+
|
89 |
+
# Copy the file to the DATA_DIR
|
90 |
+
dest_path = Path(DATA_DIR) / "saved_pdf.pdf"
|
91 |
+
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
92 |
+
temp_path.replace(dest_path)
|
93 |
+
|
94 |
+
# Process the uploaded PDF
|
95 |
+
if data_ingestion():
|
96 |
+
return "PDF processed successfully. You can now ask questions about its content."
|
97 |
+
else:
|
98 |
+
return "Failed to process the PDF. Please try uploading again."
|
99 |
+
except Exception as e:
|
100 |
+
logger.error(f"Error processing file: {str(e)}")
|
101 |
+
return f"An error occurred while processing the file: {str(e)}"
|
102 |
|
103 |
def chat_function(message, history):
|
104 |
response = handle_query(message)
|