Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
4 |
from llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding
|
|
|
5 |
from llama_index.llms.groq import Groq
|
6 |
from llama_parse import LlamaParse
|
|
|
|
|
7 |
|
8 |
# API keys
|
9 |
llama_cloud_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
@@ -14,7 +18,18 @@ if not (llama_cloud_key and groq_key and mxbai_key):
|
|
14 |
|
15 |
# Model names
|
16 |
llm_model_name = "llama-3.1-70b-versatile"
|
17 |
-
embed_model_name = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Initialize the parser
|
20 |
parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
|
@@ -37,8 +52,20 @@ file_extractor = {
|
|
37 |
}
|
38 |
|
39 |
# Initialize models with error handling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
try:
|
41 |
-
embed_model =
|
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)}")
|
@@ -61,14 +88,20 @@ def load_files(file_path: str):
|
|
61 |
input_files=[file_path],
|
62 |
file_extractor=file_extractor
|
63 |
).load_data()
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
except Exception as e:
|
71 |
-
return f"Error
|
72 |
|
73 |
# Respond function
|
74 |
def respond(message, history):
|
@@ -115,7 +148,7 @@ with gr.Blocks(
|
|
115 |
with gr.Column(scale=3):
|
116 |
chatbot = gr.ChatInterface(
|
117 |
fn=respond,
|
118 |
-
chatbot=gr.Chatbot(height=300, type="messages"),
|
119 |
theme="soft",
|
120 |
show_progress="full",
|
121 |
textbox=gr.Textbox(
|
|
|
1 |
import os
|
2 |
+
import time
|
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.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
from llama_index.llms.groq import Groq
|
8 |
from llama_parse import LlamaParse
|
9 |
+
import mixedbread_ai
|
10 |
+
from mixedbread_ai.core.api_error import ApiError
|
11 |
|
12 |
# API keys
|
13 |
llama_cloud_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
|
|
18 |
|
19 |
# Model names
|
20 |
llm_model_name = "llama-3.1-70b-versatile"
|
21 |
+
embed_model_name = "mxbai-embed-large-v1" # Mixedbread AI model
|
22 |
+
fallback_embed_model = "sentence-transformers/all-MiniLM-L6-v2" # Fallback model
|
23 |
+
|
24 |
+
# Configure Mixedbread AI SDK
|
25 |
+
mixedbread_config = mixedbread_ai.Configuration(
|
26 |
+
api_key=mxbai_key,
|
27 |
+
retry_on=[503], # Retry on 503 Service Unavailable
|
28 |
+
max_retries=3,
|
29 |
+
retry_delay=2.0, # Seconds between retries
|
30 |
+
timeout=30.0, # Request timeout
|
31 |
+
)
|
32 |
+
mixedbread_client = mixedbread_ai.Client(configuration=mixedbread_config)
|
33 |
|
34 |
# Initialize the parser
|
35 |
parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
|
|
|
52 |
}
|
53 |
|
54 |
# Initialize models with error handling
|
55 |
+
def initialize_embed_model():
|
56 |
+
try:
|
57 |
+
return MixedbreadAIEmbedding(
|
58 |
+
api_key=mxbai_key,
|
59 |
+
model_name=embed_model_name,
|
60 |
+
mxbai_client=mixedbread_client, # Use configured SDK client
|
61 |
+
)
|
62 |
+
except Exception as e:
|
63 |
+
print(f"Failed to initialize Mixedbread AI embedding: {str(e)}")
|
64 |
+
print("Falling back to local HuggingFace embedding model.")
|
65 |
+
return HuggingFaceEmbedding(model_name=fallback_embed_model)
|
66 |
+
|
67 |
try:
|
68 |
+
embed_model = initialize_embed_model()
|
69 |
llm = Groq(model=llm_model_name, api_key=groq_key)
|
70 |
except Exception as e:
|
71 |
raise RuntimeError(f"Failed to initialize models: {str(e)}")
|
|
|
88 |
input_files=[file_path],
|
89 |
file_extractor=file_extractor
|
90 |
).load_data()
|
91 |
+
|
92 |
+
try:
|
93 |
+
vector_index = VectorStoreIndex.from_documents(
|
94 |
+
document,
|
95 |
+
embed_model=embed_model
|
96 |
+
)
|
97 |
+
filename = os.path.basename(file_path)
|
98 |
+
return f"Ready to provide responses based on: {filename}"
|
99 |
+
except ApiError as e:
|
100 |
+
return f"Error processing file with Mixedbread AI API: {str(e)}. Status code: {e.status_code}"
|
101 |
+
except Exception as e:
|
102 |
+
return f"Unexpected error processing file: {str(e)}"
|
103 |
except Exception as e:
|
104 |
+
return f"Error loading file: {str(e)}"
|
105 |
|
106 |
# Respond function
|
107 |
def respond(message, history):
|
|
|
148 |
with gr.Column(scale=3):
|
149 |
chatbot = gr.ChatInterface(
|
150 |
fn=respond,
|
151 |
+
chatbot=gr.Chatbot(height=300, type="messages"),
|
152 |
theme="soft",
|
153 |
show_progress="full",
|
154 |
textbox=gr.Textbox(
|