kingabzpro commited on
Commit
3f027e1
·
verified ·
1 Parent(s): b73bc76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -6,7 +6,6 @@ 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
@@ -18,19 +17,9 @@ if not (llama_cloud_key and groq_key and mxbai_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")
36
 
@@ -52,17 +41,22 @@ file_extractor = {
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()
@@ -74,7 +68,7 @@ except Exception as e:
74
  vector_index = None
75
 
76
  # File processing function
77
- def load_files(file_path: str):
78
  global vector_index
79
  if not file_path:
80
  return "No file path provided. Please upload a file."
@@ -89,17 +83,22 @@ def load_files(file_path: str):
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
 
 
6
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
  from llama_index.llms.groq import Groq
8
  from llama_parse import LlamaParse
 
9
  from mixedbread_ai.core.api_error import ApiError
10
 
11
  # API keys
 
17
 
18
  # Model names
19
  llm_model_name = "llama-3.1-70b-versatile"
20
+ embed_model_name = "mixedbread-ai/mxbai-embed-large-v1"
21
  fallback_embed_model = "sentence-transformers/all-MiniLM-L6-v2" # Fallback model
22
 
 
 
 
 
 
 
 
 
 
 
23
  # Initialize the parser
24
  parser = LlamaParse(api_key=llama_cloud_key, result_type="markdown")
25
 
 
41
  }
42
 
43
  # Initialize models with error handling
44
+ def initialize_embed_model(max_retries=3, delay=2):
45
+ for attempt in range(max_retries):
46
+ try:
47
+ return MixedbreadAIEmbedding(api_key=mxbai_key, model_name=embed_model_name)
48
+ except ApiError as e:
49
+ if attempt == max_retries - 1:
50
+ print(f"Failed to initialize Mixedbread AI embedding after {max_retries} attempts: {str(e)}")
51
+ print("Falling back to local HuggingFace embedding model.")
52
+ return HuggingFaceEmbedding(model_name=fallback_embed_model)
53
+ time.sleep(delay)
54
+ except Exception as e:
55
+ print(f"Unexpected error initializing embedding model: {str(e)}")
56
+ if attempt == max_retries - 1:
57
+ print("Falling back to local HuggingFace embedding model.")
58
+ return HuggingFaceEmbedding(model_name=fallback_embed_model)
59
+ time.sleep(delay)
60
 
61
  try:
62
  embed_model = initialize_embed_model()
 
68
  vector_index = None
69
 
70
  # File processing function
71
+ def load_files(file_path: str, max_retries=3, delay=2):
72
  global vector_index
73
  if not file_path:
74
  return "No file path provided. Please upload a file."
 
83
  file_extractor=file_extractor
84
  ).load_data()
85
 
86
+ # Retry logic for creating vector index
87
+ for attempt in range(max_retries):
88
+ try:
89
+ vector_index = VectorStoreIndex.from_documents(
90
+ document,
91
+ embed_model=embed_model
92
+ )
93
+ filename = os.path.basename(file_path)
94
+ return f"Ready to provide responses based on: {filename}"
95
+ except ApiError as e:
96
+ if attempt == max_retries - 1:
97
+ return f"Error processing file after {max_retries} attempts: {str(e)}"
98
+ print(f"Attempt {attempt + 1} failed: {str(e)}. Retrying in {delay} seconds...")
99
+ time.sleep(delay)
100
+ except Exception as e:
101
+ return f"Unexpected error processing file: {str(e)}"
102
  except Exception as e:
103
  return f"Error loading file: {str(e)}"
104