Spaces:
Running
Running
Sarath0x8f
commited on
Commit
•
8ef6cb8
1
Parent(s):
9c636d4
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Initialize the LLM and parser
|
14 |
+
llm = HuggingFaceInferenceAPI(
|
15 |
+
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
16 |
+
token=os.getenv("TOKEN")
|
17 |
+
)
|
18 |
+
|
19 |
+
parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
|
20 |
+
file_extractor = {'.pdf': parser, '.docx': parser, '.doc': parser}
|
21 |
+
|
22 |
+
# Embedding model and index initialization (to be populated by uploaded files)
|
23 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
24 |
+
|
25 |
+
# Global variable to store documents loaded from user-uploaded files
|
26 |
+
vector_index = None
|
27 |
+
|
28 |
+
# File processing function
|
29 |
+
def load_files(file_path: str):
|
30 |
+
try:
|
31 |
+
global vector_index
|
32 |
+
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
33 |
+
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
34 |
+
print(f"parsing done {file_path}")
|
35 |
+
filename = os.path.basename(file_path)
|
36 |
+
return f"Ready to give response on give {filename}"
|
37 |
+
except Exception as e:
|
38 |
+
return f"An error occurred {e}"
|
39 |
+
|
40 |
+
def respond(message, history):
|
41 |
+
try:
|
42 |
+
query_engine = vector_index.as_query_engine(llm=llm)
|
43 |
+
bot_message = query_engine.query(message)
|
44 |
+
# output = ""
|
45 |
+
# for chr in bot_message:
|
46 |
+
# output += chr
|
47 |
+
# yield output
|
48 |
+
print(f"{datetime.now()}::message=>{str(bot_message)}")
|
49 |
+
return str(bot_message)
|
50 |
+
except Exception as e:
|
51 |
+
if e == "'NoneType' object has no attribute 'as_query_engine'":
|
52 |
+
return "upload file"
|
53 |
+
return f"an error occurred {e}"
|
54 |
+
|
55 |
+
# UI Setup
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
with gr.Row():
|
58 |
+
with gr.Column(scale=1):
|
59 |
+
file_input = gr.File(file_count="single", type='filepath')
|
60 |
+
with gr.Column():
|
61 |
+
clear = gr.ClearButton()
|
62 |
+
btn = gr.Button("Submit", variant='primary')
|
63 |
+
output = gr.Text(label='Vector Index')
|
64 |
+
with gr.Column(scale=2):
|
65 |
+
gr.ChatInterface(fn=respond,
|
66 |
+
chatbot=gr.Chatbot(height=500),
|
67 |
+
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
|
68 |
+
examples=["summarize the document"]
|
69 |
+
)
|
70 |
+
|
71 |
+
# Action on button click to process file and load into index
|
72 |
+
btn.click(fn=load_files, inputs=file_input, outputs=output)
|
73 |
+
clear.click(lambda: [None]*2, outputs=[file_input, output])
|
74 |
+
|
75 |
+
|
76 |
+
# Launch the demo with public link option
|
77 |
+
if __name__ == "__main__":
|
78 |
+
demo.launch(share=True)
|