Spaces:
Sleeping
Sleeping
Deploying the App
Browse files- .gitignore +1 -0
- Dockerfile +24 -0
- app.py +136 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dockerfile
|
| 2 |
+
|
| 3 |
+
# Use the official Python image with the desired version
|
| 4 |
+
FROM python:3.9-slim
|
| 5 |
+
|
| 6 |
+
# Set the working directory inside the container
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
# Copy the requirements file to the working directory
|
| 10 |
+
COPY requirements.txt /app
|
| 11 |
+
|
| 12 |
+
# Install the dependencies
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Copy the rest of the application code to the working directory
|
| 16 |
+
COPY app.py /app
|
| 17 |
+
|
| 18 |
+
# Expose the port that Gradio will run on (default is 7860)
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 22 |
+
|
| 23 |
+
# Command to run your application
|
| 24 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
| 5 |
+
from llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding
|
| 6 |
+
from llama_index.llms.groq import Groq
|
| 7 |
+
from llama_parse import LlamaParse
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# API keys
|
| 13 |
+
llama_cloud_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
| 14 |
+
groq_key = os.environ.get("GROQ_API_KEY")
|
| 15 |
+
mxbai_key = os.environ.get("MXBAI_API_KEY")
|
| 16 |
+
print(f"LLAMA Cloud API Key: {llama_cloud_key}")
|
| 17 |
+
print(f"GROQ API Key: {groq_key}")
|
| 18 |
+
print(f"Mixedbread AI API Key: {mxbai_key}")
|
| 19 |
+
|
| 20 |
+
if not (llama_cloud_key and groq_key and mxbai_key):
|
| 21 |
+
raise ValueError(
|
| 22 |
+
"API Keys not found! Ensure they are passed to the Docker container."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# models name
|
| 26 |
+
# llm_model_name = "llama-3.1-70b-versatile"
|
| 27 |
+
embed_model_name = "mixedbread-ai/mxbai-embed-large-v1"
|
| 28 |
+
|
| 29 |
+
# Initialize the parser
|
| 30 |
+
parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
|
| 31 |
+
|
| 32 |
+
# Define file extractor with various common extensions
|
| 33 |
+
file_extractor = {
|
| 34 |
+
".pdf": parser,
|
| 35 |
+
".docx": parser,
|
| 36 |
+
".doc": parser,
|
| 37 |
+
".txt": parser,
|
| 38 |
+
".csv": parser,
|
| 39 |
+
".xlsx": parser,
|
| 40 |
+
".pptx": parser,
|
| 41 |
+
".html": parser,
|
| 42 |
+
".jpg": parser,
|
| 43 |
+
".jpeg": parser,
|
| 44 |
+
".png": parser,
|
| 45 |
+
".webp": parser,
|
| 46 |
+
".svg": parser,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Initialize the embedding model
|
| 50 |
+
embed_model = MixedbreadAIEmbedding(api_key=mxbai_key, model_name=embed_model_name)
|
| 51 |
+
|
| 52 |
+
# Initialize the LLM
|
| 53 |
+
llm = Groq(model="llama3-70b-8192", api_key=groq_key)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# File processing function
|
| 57 |
+
def load_files(file_path: str):
|
| 58 |
+
global vector_index
|
| 59 |
+
if not file_path:
|
| 60 |
+
return "No file path provided. Please upload a file."
|
| 61 |
+
|
| 62 |
+
valid_extensions = ', '.join(file_extractor.keys())
|
| 63 |
+
if not any(file_path.endswith(ext) for ext in file_extractor):
|
| 64 |
+
return f"The parser can only parse the following file types: {valid_extensions}"
|
| 65 |
+
|
| 66 |
+
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
| 67 |
+
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
| 68 |
+
print(f"Parsing completed for: {file_path}")
|
| 69 |
+
filename = os.path.basename(file_path)
|
| 70 |
+
return f"Ready to provide responses based on: {filename}"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Respond function
|
| 74 |
+
def respond(message, history):
|
| 75 |
+
try:
|
| 76 |
+
# Use the preloaded LLM
|
| 77 |
+
query_engine = vector_index.as_query_engine(streaming=True, llm=llm)
|
| 78 |
+
streaming_response = query_engine.query(message)
|
| 79 |
+
partial_text = ""
|
| 80 |
+
for new_text in streaming_response.response_gen:
|
| 81 |
+
partial_text += new_text
|
| 82 |
+
# Yield an empty string to cleanup the message textbox and the updated conversation history
|
| 83 |
+
yield partial_text
|
| 84 |
+
except (AttributeError, NameError):
|
| 85 |
+
print("An error occurred while processing your request.")
|
| 86 |
+
yield "Please upload the file to begin chat."
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Clear function
|
| 90 |
+
def clear_state():
|
| 91 |
+
global vector_index
|
| 92 |
+
vector_index = None
|
| 93 |
+
return [None, None, None]
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# UI Setup
|
| 97 |
+
with gr.Blocks(
|
| 98 |
+
theme=gr.themes.Default(
|
| 99 |
+
primary_hue="green",
|
| 100 |
+
secondary_hue="blue",
|
| 101 |
+
font=[gr.themes.GoogleFont("Poppins")],
|
| 102 |
+
),
|
| 103 |
+
css="footer {visibility: hidden}",
|
| 104 |
+
) as demo:
|
| 105 |
+
gr.Markdown("# DataCamp Doc Q&A 🤖📃")
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
file_input = gr.File(
|
| 109 |
+
file_count="single", type="filepath", label="Upload Document"
|
| 110 |
+
)
|
| 111 |
+
with gr.Row():
|
| 112 |
+
btn = gr.Button("Submit", variant="primary")
|
| 113 |
+
clear = gr.Button("Clear")
|
| 114 |
+
output = gr.Textbox(label="Status")
|
| 115 |
+
with gr.Column(scale=3):
|
| 116 |
+
chatbot = gr.ChatInterface(
|
| 117 |
+
fn=respond,
|
| 118 |
+
chatbot=gr.Chatbot(height=300),
|
| 119 |
+
theme="soft",
|
| 120 |
+
show_progress="full",
|
| 121 |
+
textbox=gr.Textbox(
|
| 122 |
+
placeholder="Ask questions about the uploaded document!",
|
| 123 |
+
container=False,
|
| 124 |
+
),
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Set up Gradio interactions
|
| 128 |
+
btn.click(fn=load_files, inputs=file_input, outputs=output)
|
| 129 |
+
clear.click(
|
| 130 |
+
fn=clear_state, # Use the clear_state function
|
| 131 |
+
outputs=[file_input, output],
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Launch the demo
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
llama-index-embeddings-mixedbreadai
|
| 3 |
+
llama-index-llms-groq
|
| 4 |
+
llama-index
|