Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 4 |
+
from llama_parse import LlamaParse
|
| 5 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Predefined model selection
|
| 15 |
+
selected_llm_model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 16 |
+
selected_embed_model_name = "BAAI/bge-small-en-v1.5"
|
| 17 |
+
vector_index = None
|
| 18 |
+
|
| 19 |
+
# Initialize the parser
|
| 20 |
+
parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
|
| 21 |
+
file_extractor = {
|
| 22 |
+
'.pdf': parser,
|
| 23 |
+
'.docx': parser,
|
| 24 |
+
'.doc': parser,
|
| 25 |
+
'.txt': parser,
|
| 26 |
+
'.csv': parser,
|
| 27 |
+
'.xlsx': parser,
|
| 28 |
+
'.pptx': parser,
|
| 29 |
+
'.html': parser,
|
| 30 |
+
'.jpg': parser,
|
| 31 |
+
'.jpeg': parser,
|
| 32 |
+
'.png': parser,
|
| 33 |
+
'.webp': parser,
|
| 34 |
+
'.svg': parser,
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# File processing function
|
| 38 |
+
def load_files(file_path: str):
|
| 39 |
+
try:
|
| 40 |
+
global vector_index
|
| 41 |
+
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
| 42 |
+
embed_model = HuggingFaceEmbedding(model_name=selected_embed_model_name)
|
| 43 |
+
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
| 44 |
+
print(f"Parsing done for {file_path}")
|
| 45 |
+
filename = os.path.basename(file_path)
|
| 46 |
+
return f"File upload status: {filename} is ready to write"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"An error occurred: {e}"
|
| 49 |
+
|
| 50 |
+
# Respond function
|
| 51 |
+
def respond(message, history):
|
| 52 |
+
try:
|
| 53 |
+
llm = HuggingFaceInferenceAPI(
|
| 54 |
+
model_name=selected_llm_model_name,
|
| 55 |
+
contextWindow=8192,
|
| 56 |
+
maxTokens=1024,
|
| 57 |
+
temperature=0.3,
|
| 58 |
+
topP=0.9,
|
| 59 |
+
frequencyPenalty=0.5,
|
| 60 |
+
presencePenalty=0.5,
|
| 61 |
+
token=os.getenv("TOKEN")
|
| 62 |
+
)
|
| 63 |
+
query_engine = vector_index.as_query_engine(llm=llm)
|
| 64 |
+
bot_message = query_engine.query(message)
|
| 65 |
+
print(f"\n{datetime.now()}:{selected_llm_model_name}:: {message} --> {str(bot_message)}\n")
|
| 66 |
+
return f"{selected_llm_model_name}:\n{str(bot_message)}"
|
| 67 |
+
except Exception as e:
|
| 68 |
+
if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
|
| 69 |
+
return "Please upload a file."
|
| 70 |
+
return f"An error occurred: {e}"
|
| 71 |
+
|
| 72 |
+
# UI Setup
|
| 73 |
+
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
|
| 74 |
+
gr.Markdown("# DocBot📄🤖")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column(scale=1):
|
| 77 |
+
file_input = gr.File(file_count="single", type='filepath', label="Upload document")
|
| 78 |
+
btn = gr.Button("Submit", variant='primary')
|
| 79 |
+
clear = gr.ClearButton()
|
| 80 |
+
output = gr.Text(label='File upload status')
|
| 81 |
+
with gr.Column(scale=3):
|
| 82 |
+
gr.ChatInterface(
|
| 83 |
+
fn=respond,
|
| 84 |
+
chatbot=gr.Chatbot(height=500),
|
| 85 |
+
theme="soft",
|
| 86 |
+
show_progress='full',
|
| 87 |
+
textbox=gr.Textbox(placeholder="Ask me questions on the uploaded document!", container=False)
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Set up Gradio interactions
|
| 91 |
+
btn.click(fn=load_files, inputs=file_input, outputs=output)
|
| 92 |
+
clear.click(lambda: [None] * 2, outputs=[file_input, output])
|
| 93 |
+
|
| 94 |
+
# Launch the demo
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
demo.launch()
|