Spaces:
Sleeping
Sleeping
Srinivasulu kethanaboina
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
import gradio as gr
|
4 |
-
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
5 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
-
|
9 |
load_dotenv()
|
|
|
10 |
# Configure the Llama index settings
|
11 |
Settings.llm = HuggingFaceInferenceAPI(
|
12 |
model_name="google/gemma-1.1-7b-it",
|
@@ -28,6 +29,7 @@ PDF_DIRECTORY = 'data' # Changed to the directory containing PDFs
|
|
28 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
29 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
30 |
|
|
|
31 |
def data_ingestion_from_directory():
|
32 |
# Use SimpleDirectoryReader on the directory containing the PDF files
|
33 |
documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
@@ -35,13 +37,14 @@ def data_ingestion_from_directory():
|
|
35 |
index = VectorStoreIndex.from_documents(documents)
|
36 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
37 |
|
38 |
-
|
|
|
39 |
chat_text_qa_msgs = [
|
40 |
(
|
41 |
"user",
|
42 |
"""
|
43 |
You are a RedfernsTech chatbot whose aim is to provide better service to the user, utilizing provided context to deliver answers.
|
44 |
-
and collect
|
45 |
{context_str}
|
46 |
Question:
|
47 |
{query_str}
|
@@ -58,43 +61,32 @@ def handle_query(query):
|
|
58 |
answer = query_engine.query(query)
|
59 |
|
60 |
if hasattr(answer, 'response'):
|
61 |
-
|
62 |
elif isinstance(answer, dict) and 'response' in answer:
|
63 |
-
|
64 |
else:
|
65 |
-
|
66 |
|
67 |
-
|
|
|
68 |
|
69 |
-
|
|
|
70 |
print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
|
71 |
data_ingestion_from_directory()
|
72 |
|
73 |
-
|
74 |
query = "How do I use the RedfernsTech Q&A assistant?"
|
75 |
print("Query:", query)
|
76 |
-
response = handle_query(query)
|
77 |
print("Answer:", response)
|
78 |
-
# prompt: create a gradio chatbot for this
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
# Define the input and output components for the Gradio interface
|
83 |
-
input_component = gr.Textbox(
|
84 |
-
show_label=False,
|
85 |
-
placeholder="Ask me anything about the document..."
|
86 |
-
)
|
87 |
-
|
88 |
-
output_component = gr.Textbox()
|
89 |
|
90 |
-
# Create the Gradio interface
|
91 |
-
|
92 |
fn=handle_query,
|
93 |
-
inputs=input_component,
|
94 |
-
outputs=output_component,
|
95 |
title="RedfernsTech Q&A Chatbot",
|
96 |
-
description="Ask me anything about the uploaded
|
|
|
97 |
)
|
98 |
|
99 |
-
|
100 |
-
interface.launch()
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
import gradio as gr
|
4 |
+
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
5 |
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
+
|
9 |
load_dotenv()
|
10 |
+
|
11 |
# Configure the Llama index settings
|
12 |
Settings.llm = HuggingFaceInferenceAPI(
|
13 |
model_name="google/gemma-1.1-7b-it",
|
|
|
29 |
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
30 |
os.makedirs(PERSIST_DIR, exist_ok=True)
|
31 |
|
32 |
+
|
33 |
def data_ingestion_from_directory():
|
34 |
# Use SimpleDirectoryReader on the directory containing the PDF files
|
35 |
documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
|
|
37 |
index = VectorStoreIndex.from_documents(documents)
|
38 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
39 |
|
40 |
+
|
41 |
+
def handle_query(query, history):
|
42 |
chat_text_qa_msgs = [
|
43 |
(
|
44 |
"user",
|
45 |
"""
|
46 |
You are a RedfernsTech chatbot whose aim is to provide better service to the user, utilizing provided context to deliver answers.
|
47 |
+
and collect some basic information first like name, email, company name.
|
48 |
{context_str}
|
49 |
Question:
|
50 |
{query_str}
|
|
|
61 |
answer = query_engine.query(query)
|
62 |
|
63 |
if hasattr(answer, 'response'):
|
64 |
+
response = answer.response
|
65 |
elif isinstance(answer, dict) and 'response' in answer:
|
66 |
+
response = answer['response']
|
67 |
else:
|
68 |
+
response = "Sorry, I couldn't find an answer."
|
69 |
|
70 |
+
history.append((query, response))
|
71 |
+
return response, history
|
72 |
|
73 |
+
|
74 |
+
# Example usage
|
75 |
print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
|
76 |
data_ingestion_from_directory()
|
77 |
|
78 |
+
# Example query
|
79 |
query = "How do I use the RedfernsTech Q&A assistant?"
|
80 |
print("Query:", query)
|
81 |
+
response = handle_query(query, [])
|
82 |
print("Answer:", response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# Create the Gradio chatbot interface with history
|
85 |
+
chatbot = gr.ChatInterface(
|
86 |
fn=handle_query,
|
|
|
|
|
87 |
title="RedfernsTech Q&A Chatbot",
|
88 |
+
description="Ask me anything about the uploaded documents.",
|
89 |
+
cache_examples=True, # Enable history caching
|
90 |
)
|
91 |
|
92 |
+
chatbot.launch()
|
|