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",
|
|
@@ -41,7 +42,7 @@ def handle_query(query):
|
|
| 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 the some basic
|
| 45 |
{context_str}
|
| 46 |
Question:
|
| 47 |
{query_str}
|
|
@@ -58,26 +59,29 @@ 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(
|
|
@@ -87,13 +91,19 @@ input_component = gr.Textbox(
|
|
| 87 |
|
| 88 |
output_component = gr.Textbox()
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Create the Gradio interface
|
| 91 |
interface = gr.Interface(
|
| 92 |
-
fn=
|
| 93 |
inputs=input_component,
|
| 94 |
outputs=output_component,
|
| 95 |
title="RedfernsTech Q&A Chatbot",
|
| 96 |
-
description="Ask me anything about the uploaded document."
|
| 97 |
)
|
| 98 |
|
| 99 |
# Launch the Gradio interface
|
|
|
|
| 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",
|
|
|
|
| 42 |
"user",
|
| 43 |
"""
|
| 44 |
You are a RedfernsTech chatbot whose aim is to provide better service to the user, utilizing provided context to deliver answers.
|
| 45 |
+
and collect the some basic information first also name, email, company name
|
| 46 |
{context_str}
|
| 47 |
Question:
|
| 48 |
{query_str}
|
|
|
|
| 59 |
answer = query_engine.query(query)
|
| 60 |
|
| 61 |
if hasattr(answer, 'response'):
|
| 62 |
+
response = answer.response
|
| 63 |
elif isinstance(answer, dict) and 'response' in answer:
|
| 64 |
+
response = answer['response']
|
| 65 |
else:
|
| 66 |
+
response = "Sorry, I couldn't find an answer."
|
| 67 |
+
|
| 68 |
+
# Append the query and response to chat history
|
| 69 |
+
chat_history.append((query, response))
|
| 70 |
|
| 71 |
+
return response
|
| 72 |
|
| 73 |
+
# Initialize chat history
|
| 74 |
+
chat_history = []
|
| 75 |
+
|
| 76 |
+
# Example usage: Process PDF ingestion from directory
|
| 77 |
print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
|
| 78 |
data_ingestion_from_directory()
|
| 79 |
|
| 80 |
+
# Example query
|
| 81 |
query = "How do I use the RedfernsTech Q&A assistant?"
|
| 82 |
print("Query:", query)
|
| 83 |
response = handle_query(query)
|
| 84 |
print("Answer:", response)
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Define the input and output components for the Gradio interface
|
| 87 |
input_component = gr.Textbox(
|
|
|
|
| 91 |
|
| 92 |
output_component = gr.Textbox()
|
| 93 |
|
| 94 |
+
# Function to add chat history to output
|
| 95 |
+
def chat_with_history(query):
|
| 96 |
+
response = handle_query(query)
|
| 97 |
+
history_str = "\n\n".join([f"Query:\n{q}\nAnswer:\n{a}" for q, a in chat_history])
|
| 98 |
+
return f"{response}\n\nChat History:\n\n{history_str}"
|
| 99 |
+
|
| 100 |
# Create the Gradio interface
|
| 101 |
interface = gr.Interface(
|
| 102 |
+
fn=chat_with_history,
|
| 103 |
inputs=input_component,
|
| 104 |
outputs=output_component,
|
| 105 |
title="RedfernsTech Q&A Chatbot",
|
| 106 |
+
description="Ask me anything about the uploaded document. View chat history below."
|
| 107 |
)
|
| 108 |
|
| 109 |
# Launch the Gradio interface
|