Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 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 |
-
from mixedbread_ai.core.api_error import ApiError
|
| 10 |
|
| 11 |
# API keys
|
| 12 |
llama_cloud_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
|
@@ -18,7 +15,6 @@ if not (llama_cloud_key and groq_key and mxbai_key):
|
|
| 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")
|
|
@@ -41,25 +37,8 @@ file_extractor = {
|
|
| 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 =
|
| 63 |
llm = Groq(model=llm_model_name, api_key=groq_key)
|
| 64 |
except Exception as e:
|
| 65 |
raise RuntimeError(f"Failed to initialize models: {str(e)}")
|
|
@@ -68,7 +47,7 @@ except Exception as e:
|
|
| 68 |
vector_index = None
|
| 69 |
|
| 70 |
# File processing function
|
| 71 |
-
def load_files(file_path: str
|
| 72 |
global vector_index
|
| 73 |
if not file_path:
|
| 74 |
return "No file path provided. Please upload a file."
|
|
@@ -82,25 +61,14 @@ def load_files(file_path: str, max_retries=3, delay=2):
|
|
| 82 |
input_files=[file_path],
|
| 83 |
file_extractor=file_extractor
|
| 84 |
).load_data()
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 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
|
| 104 |
|
| 105 |
# Respond function
|
| 106 |
def respond(message, history):
|
|
@@ -147,7 +115,7 @@ with gr.Blocks(
|
|
| 147 |
with gr.Column(scale=3):
|
| 148 |
chatbot = gr.ChatInterface(
|
| 149 |
fn=respond,
|
| 150 |
-
chatbot=gr.Chatbot(height=300, type="messages"),
|
| 151 |
theme="soft",
|
| 152 |
show_progress="full",
|
| 153 |
textbox=gr.Textbox(
|
|
|
|
| 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")
|
|
|
|
| 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")
|
|
|
|
| 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)}")
|
|
|
|
| 47 |
vector_index = None
|
| 48 |
|
| 49 |
# File processing function
|
| 50 |
+
def load_files(file_path: str):
|
| 51 |
global vector_index
|
| 52 |
if not file_path:
|
| 53 |
return "No file path provided. Please upload a file."
|
|
|
|
| 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):
|
|
|
|
| 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(
|