Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
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
|
@@ -11,18 +10,16 @@ llama_cloud_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
|
11 |
groq_key = os.environ.get("GROQ_API_KEY")
|
12 |
mxbai_key = os.environ.get("MXBAI_API_KEY")
|
13 |
if not (llama_cloud_key and groq_key and mxbai_key):
|
14 |
-
raise ValueError(
|
15 |
-
"API Keys not found! Ensure they are passed to the Docker container."
|
16 |
-
)
|
17 |
|
18 |
-
#
|
19 |
llm_model_name = "llama-3.1-70b-versatile"
|
20 |
embed_model_name = "mixedbread-ai/mxbai-embed-large-v1"
|
21 |
|
22 |
# Initialize the parser
|
23 |
parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
|
24 |
|
25 |
-
# Define file extractor
|
26 |
file_extractor = {
|
27 |
".pdf": parser,
|
28 |
".docx": parser,
|
@@ -39,13 +36,15 @@ file_extractor = {
|
|
39 |
".svg": parser,
|
40 |
}
|
41 |
|
42 |
-
# Initialize
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
|
|
|
|
49 |
|
50 |
# File processing function
|
51 |
def load_files(file_path: str):
|
@@ -57,35 +56,40 @@ def load_files(file_path: str):
|
|
57 |
if not any(file_path.endswith(ext) for ext in file_extractor):
|
58 |
return f"The parser can only parse the following file types: {valid_extensions}"
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Respond function
|
68 |
def respond(message, history):
|
|
|
|
|
|
|
69 |
try:
|
70 |
-
# Use the preloaded LLM
|
71 |
query_engine = vector_index.as_query_engine(streaming=True, llm=llm)
|
72 |
streaming_response = query_engine.query(message)
|
73 |
partial_text = ""
|
74 |
for new_text in streaming_response.response_gen:
|
75 |
partial_text += new_text
|
76 |
-
# Yield an empty string to cleanup the message textbox and the updated conversation history
|
77 |
yield partial_text
|
78 |
-
except
|
79 |
-
|
80 |
-
yield "Please upload the file to begin chat."
|
81 |
-
|
82 |
|
83 |
# Clear function
|
84 |
def clear_state():
|
85 |
global vector_index
|
86 |
vector_index = None
|
87 |
-
return
|
88 |
-
|
89 |
|
90 |
# UI Setup
|
91 |
with gr.Blocks(
|
@@ -100,7 +104,9 @@ with gr.Blocks(
|
|
100 |
with gr.Row():
|
101 |
with gr.Column(scale=1):
|
102 |
file_input = gr.File(
|
103 |
-
file_count="single",
|
|
|
|
|
104 |
)
|
105 |
with gr.Row():
|
106 |
btn = gr.Button("Submit", variant="primary")
|
@@ -109,7 +115,7 @@ with gr.Blocks(
|
|
109 |
with gr.Column(scale=3):
|
110 |
chatbot = gr.ChatInterface(
|
111 |
fn=respond,
|
112 |
-
chatbot=gr.Chatbot(height=300),
|
113 |
theme="soft",
|
114 |
show_progress="full",
|
115 |
textbox=gr.Textbox(
|
@@ -121,10 +127,13 @@ with gr.Blocks(
|
|
121 |
# Set up Gradio interactions
|
122 |
btn.click(fn=load_files, inputs=file_input, outputs=output)
|
123 |
clear.click(
|
124 |
-
fn=clear_state,
|
125 |
-
outputs=[file_input, output],
|
126 |
)
|
127 |
|
128 |
# Launch the demo
|
129 |
if __name__ == "__main__":
|
130 |
-
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
4 |
from llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding
|
|
|
10 |
groq_key = os.environ.get("GROQ_API_KEY")
|
11 |
mxbai_key = os.environ.get("MXBAI_API_KEY")
|
12 |
if not (llama_cloud_key and groq_key and mxbai_key):
|
13 |
+
raise ValueError("API Keys not found! Ensure they are passed to the Docker container.")
|
|
|
|
|
14 |
|
15 |
+
# Model names
|
16 |
llm_model_name = "llama-3.1-70b-versatile"
|
17 |
embed_model_name = "mixedbread-ai/mxbai-embed-large-v1"
|
18 |
|
19 |
# Initialize the parser
|
20 |
parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
|
21 |
|
22 |
+
# Define file extractor
|
23 |
file_extractor = {
|
24 |
".pdf": parser,
|
25 |
".docx": parser,
|
|
|
36 |
".svg": parser,
|
37 |
}
|
38 |
|
39 |
+
# Initialize models with error handling
|
40 |
+
try:
|
41 |
+
embed_model = MixedbreadAIEmbedding(api_key=mxbai_key, model_name=embed_model_name)
|
42 |
+
llm = Groq(model=llm_model_name, api_key=groq_key)
|
43 |
+
except Exception as e:
|
44 |
+
raise RuntimeError(f"Failed to initialize models: {str(e)}")
|
45 |
|
46 |
+
# Global variable for vector index
|
47 |
+
vector_index = None
|
48 |
|
49 |
# File processing function
|
50 |
def load_files(file_path: str):
|
|
|
56 |
if not any(file_path.endswith(ext) for ext in file_extractor):
|
57 |
return f"The parser can only parse the following file types: {valid_extensions}"
|
58 |
|
59 |
+
try:
|
60 |
+
document = SimpleDirectoryReader(
|
61 |
+
input_files=[file_path],
|
62 |
+
file_extractor=file_extractor
|
63 |
+
).load_data()
|
64 |
+
vector_index = VectorStoreIndex.from_documents(
|
65 |
+
document,
|
66 |
+
embed_model=embed_model
|
67 |
+
)
|
68 |
+
filename = os.path.basename(file_path)
|
69 |
+
return f"Ready to provide responses based on: {filename}"
|
70 |
+
except Exception as e:
|
71 |
+
return f"Error processing file: {str(e)}"
|
72 |
|
73 |
# Respond function
|
74 |
def respond(message, history):
|
75 |
+
if not vector_index:
|
76 |
+
return "Please upload a file first."
|
77 |
+
|
78 |
try:
|
|
|
79 |
query_engine = vector_index.as_query_engine(streaming=True, llm=llm)
|
80 |
streaming_response = query_engine.query(message)
|
81 |
partial_text = ""
|
82 |
for new_text in streaming_response.response_gen:
|
83 |
partial_text += new_text
|
|
|
84 |
yield partial_text
|
85 |
+
except Exception as e:
|
86 |
+
yield f"Error processing query: {str(e)}"
|
|
|
|
|
87 |
|
88 |
# Clear function
|
89 |
def clear_state():
|
90 |
global vector_index
|
91 |
vector_index = None
|
92 |
+
return None, None, None
|
|
|
93 |
|
94 |
# UI Setup
|
95 |
with gr.Blocks(
|
|
|
104 |
with gr.Row():
|
105 |
with gr.Column(scale=1):
|
106 |
file_input = gr.File(
|
107 |
+
file_count="single",
|
108 |
+
type="filepath",
|
109 |
+
label="Upload Document"
|
110 |
)
|
111 |
with gr.Row():
|
112 |
btn = gr.Button("Submit", variant="primary")
|
|
|
115 |
with gr.Column(scale=3):
|
116 |
chatbot = gr.ChatInterface(
|
117 |
fn=respond,
|
118 |
+
chatbot=gr.Chatbot(height=300, type="messages"), # Fixed deprecated warning
|
119 |
theme="soft",
|
120 |
show_progress="full",
|
121 |
textbox=gr.Textbox(
|
|
|
127 |
# Set up Gradio interactions
|
128 |
btn.click(fn=load_files, inputs=file_input, outputs=output)
|
129 |
clear.click(
|
130 |
+
fn=clear_state,
|
131 |
+
outputs=[file_input, output, chatbot],
|
132 |
)
|
133 |
|
134 |
# Launch the demo
|
135 |
if __name__ == "__main__":
|
136 |
+
try:
|
137 |
+
demo.launch()
|
138 |
+
except Exception as e:
|
139 |
+
print(f"Failed to launch application: {str(e)}")
|