Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from langdetect import detect
|
|
|
|
| 5 |
|
| 6 |
# Load the Hugging Face token from environment variables (secrets)
|
| 7 |
token = os.environ.get("Key2") # Replace "KEY2" with your secret key name
|
|
@@ -33,6 +34,32 @@ def detect_language(text):
|
|
| 33 |
except:
|
| 34 |
return "en" # Default to English if detection fails
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Streamlit App
|
| 37 |
def main():
|
| 38 |
st.title("RAG Model with Advanced Query Translation and Indexing")
|
|
@@ -41,6 +68,10 @@ def main():
|
|
| 41 |
# Sidebar for options
|
| 42 |
st.sidebar.title("Options")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Query Translation Options
|
| 45 |
st.sidebar.header("Query Translation")
|
| 46 |
query_translation = st.sidebar.selectbox(
|
|
@@ -63,7 +94,7 @@ def main():
|
|
| 63 |
|
| 64 |
# System Prompt
|
| 65 |
st.sidebar.header("System Prompt")
|
| 66 |
-
default_system_prompt =
|
| 67 |
system_prompt = st.sidebar.text_area("System Prompt", default_system_prompt)
|
| 68 |
|
| 69 |
# Main Content
|
|
@@ -79,11 +110,14 @@ def main():
|
|
| 79 |
# Query Translation
|
| 80 |
if st.button("Apply Query Translation"):
|
| 81 |
st.write(f"**Applied Query Translation Method:** {query_translation}")
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Indexing
|
| 89 |
if st.button("Apply Indexing"):
|
|
@@ -93,11 +127,17 @@ def main():
|
|
| 93 |
if indexing_method == "ColBERT":
|
| 94 |
st.write("Indexing with ColBERT...")
|
| 95 |
|
| 96 |
-
# Query the Hugging Face API
|
| 97 |
if st.button("Generate Response"):
|
| 98 |
response = query_huggingface_api(prompt, max_new_tokens, temperature, top_k)
|
| 99 |
if response:
|
| 100 |
st.write("**Response:**", response)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
main()
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from langdetect import detect
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
|
| 7 |
# Load the Hugging Face token from environment variables (secrets)
|
| 8 |
token = os.environ.get("Key2") # Replace "KEY2" with your secret key name
|
|
|
|
| 34 |
except:
|
| 35 |
return "en" # Default to English if detection fails
|
| 36 |
|
| 37 |
+
# Function to extract text from PDF
|
| 38 |
+
def extract_text_from_pdf(pdf_file):
|
| 39 |
+
pdf_reader = PdfReader(pdf_file)
|
| 40 |
+
text = ""
|
| 41 |
+
for page in pdf_reader.pages:
|
| 42 |
+
text += page.extract_text()
|
| 43 |
+
return text
|
| 44 |
+
|
| 45 |
+
# Default system prompts for each query translation method
|
| 46 |
+
DEFAULT_SYSTEM_PROMPTS = {
|
| 47 |
+
"Multi-Query": """You are an AI language model assistant. Your task is to generate five
|
| 48 |
+
different versions of the given user question to retrieve relevant documents from a vector
|
| 49 |
+
database. By generating multiple perspectives on the user question, your goal is to help
|
| 50 |
+
the user overcome some of the limitations of the distance-based similarity search.
|
| 51 |
+
Provide these alternative questions separated by newlines. Original question: {question}""",
|
| 52 |
+
"RAG Fusion": """You are an AI language model assistant. Your task is to combine multiple
|
| 53 |
+
queries into a single, refined query to improve retrieval accuracy. Original question: {question}""",
|
| 54 |
+
"Decomposition": """You are an AI language model assistant. Your task is to break down
|
| 55 |
+
the given user question into simpler sub-questions. Provide these sub-questions separated
|
| 56 |
+
by newlines. Original question: {question}""",
|
| 57 |
+
"Step Back": """You are an AI language model assistant. Your task is to refine the given
|
| 58 |
+
user question by taking a step back and asking a more general question. Original question: {question}""",
|
| 59 |
+
"HyDE": """You are an AI language model assistant. Your task is to generate a hypothetical
|
| 60 |
+
document that would be relevant to the given user question. Original question: {question}""",
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
# Streamlit App
|
| 64 |
def main():
|
| 65 |
st.title("RAG Model with Advanced Query Translation and Indexing")
|
|
|
|
| 68 |
# Sidebar for options
|
| 69 |
st.sidebar.title("Options")
|
| 70 |
|
| 71 |
+
# PDF Upload
|
| 72 |
+
st.sidebar.header("Upload PDF")
|
| 73 |
+
pdf_file = st.sidebar.file_uploader("Upload a PDF file", type="pdf")
|
| 74 |
+
|
| 75 |
# Query Translation Options
|
| 76 |
st.sidebar.header("Query Translation")
|
| 77 |
query_translation = st.sidebar.selectbox(
|
|
|
|
| 94 |
|
| 95 |
# System Prompt
|
| 96 |
st.sidebar.header("System Prompt")
|
| 97 |
+
default_system_prompt = DEFAULT_SYSTEM_PROMPTS[query_translation]
|
| 98 |
system_prompt = st.sidebar.text_area("System Prompt", default_system_prompt)
|
| 99 |
|
| 100 |
# Main Content
|
|
|
|
| 110 |
# Query Translation
|
| 111 |
if st.button("Apply Query Translation"):
|
| 112 |
st.write(f"**Applied Query Translation Method:** {query_translation}")
|
| 113 |
+
# Format the system prompt with the user's question
|
| 114 |
+
formatted_prompt = system_prompt.format(question=prompt)
|
| 115 |
+
st.write("**Formatted System Prompt:**", formatted_prompt)
|
| 116 |
+
|
| 117 |
+
# Query the Hugging Face API for query translation
|
| 118 |
+
translated_queries = query_huggingface_api(formatted_prompt, max_new_tokens, temperature, top_k)
|
| 119 |
+
if translated_queries:
|
| 120 |
+
st.write("**Translated Queries:**", translated_queries)
|
| 121 |
|
| 122 |
# Indexing
|
| 123 |
if st.button("Apply Indexing"):
|
|
|
|
| 127 |
if indexing_method == "ColBERT":
|
| 128 |
st.write("Indexing with ColBERT...")
|
| 129 |
|
| 130 |
+
# Query the Hugging Face API for final response
|
| 131 |
if st.button("Generate Response"):
|
| 132 |
response = query_huggingface_api(prompt, max_new_tokens, temperature, top_k)
|
| 133 |
if response:
|
| 134 |
st.write("**Response:**", response)
|
| 135 |
|
| 136 |
+
# Display PDF text if uploaded
|
| 137 |
+
if pdf_file is not None:
|
| 138 |
+
st.header("PDF Content")
|
| 139 |
+
pdf_text = extract_text_from_pdf(pdf_file)
|
| 140 |
+
st.write(pdf_text)
|
| 141 |
+
|
| 142 |
if __name__ == "__main__":
|
| 143 |
main()
|