Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,9 @@ import PyPDF2
|
|
15 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
16 |
from sentence_transformers import SentenceTransformer
|
17 |
|
18 |
-
# RAG components
|
19 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
20 |
-
from langchain_community.vectorstores import FAISS
|
21 |
from langchain.schema import Document
|
22 |
from langchain.embeddings import HuggingFaceEmbeddings
|
23 |
|
@@ -53,7 +53,7 @@ def detect_language(text):
|
|
53 |
is_arabic = len(arabic_chars) > len(text) * 0.5
|
54 |
return "arabic" if is_arabic else "english"
|
55 |
|
56 |
-
#
|
57 |
def calculate_bleu(prediction, reference):
|
58 |
"""Calculate BLEU score without any NLTK dependency"""
|
59 |
# Tokenize texts using our own tokenizer
|
@@ -127,7 +127,7 @@ def calculate_f1_precision_recall(prediction, reference):
|
|
127 |
|
128 |
def evaluate_retrieval_quality(contexts, query, language):
|
129 |
"""Evaluate the quality of retrieved contexts"""
|
130 |
-
# This is a placeholder function
|
131 |
return {
|
132 |
'language_match_ratio': 1.0,
|
133 |
'source_diversity': len(set([ctx.get('source', '') for ctx in contexts])) / max(1, len(contexts)),
|
@@ -207,7 +207,7 @@ def create_vector_store(documents):
|
|
207 |
|
208 |
return vector_store
|
209 |
|
210 |
-
# Model Loading and RAG System
|
211 |
@spaces.GPU
|
212 |
def load_model_and_tokenizer():
|
213 |
"""Load the ALLaM-7B model and tokenizer with error handling"""
|
@@ -215,6 +215,13 @@ def load_model_and_tokenizer():
|
|
215 |
print(f"Loading model: {model_name}")
|
216 |
|
217 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
# First attempt with AutoTokenizer
|
219 |
tokenizer = AutoTokenizer.from_pretrained(
|
220 |
model_name,
|
@@ -231,25 +238,20 @@ def load_model_and_tokenizer():
|
|
231 |
)
|
232 |
|
233 |
print("Model loaded successfully with AutoTokenizer!")
|
|
|
234 |
|
235 |
except Exception as e:
|
236 |
print(f"First loading attempt failed: {e}")
|
237 |
-
print("Trying alternative loading approach...")
|
238 |
-
|
239 |
-
# Try with specific tokenizer class if the first attempt fails
|
240 |
-
from transformers import LlamaTokenizer
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
)
|
249 |
|
250 |
-
|
251 |
-
|
252 |
-
return model, tokenizer
|
253 |
|
254 |
def retrieve_context(query, vector_store, top_k=5):
|
255 |
"""Retrieve most relevant document chunks for a given query"""
|
@@ -690,6 +692,13 @@ def main():
|
|
690 |
print("Files in directory:", os.listdir("."))
|
691 |
print("=" * 50)
|
692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
693 |
# Initialize the system with simplified error handling
|
694 |
try:
|
695 |
# First create a very simple Gradio interface to show we're starting
|
@@ -698,7 +707,10 @@ def main():
|
|
698 |
gr.Markdown("System is initializing. This may take a few minutes...")
|
699 |
status = gr.Textbox(value="Loading resources...", label="Status")
|
700 |
|
701 |
-
|
|
|
|
|
|
|
702 |
|
703 |
# Now try the actual initialization
|
704 |
try:
|
@@ -710,12 +722,48 @@ def main():
|
|
710 |
|
711 |
print("Launching interface...")
|
712 |
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
713 |
except Exception as e:
|
714 |
print(f"Error during initialization: {e}")
|
715 |
import traceback
|
716 |
traceback.print_exc()
|
717 |
|
718 |
-
# Create a
|
719 |
with gr.Blocks(title="Vision 2030 Assistant - Error") as debug_interface:
|
720 |
gr.Markdown("# Vision 2030 Assistant - Initialization Error")
|
721 |
gr.Markdown("There was an error initializing the assistant.")
|
@@ -756,6 +804,15 @@ def main():
|
|
756 |
with gr.Blocks(title="Vision 2030 Assistant - Critical Error") as critical_error:
|
757 |
gr.Markdown("# Vision 2030 Assistant - Critical Error")
|
758 |
gr.Markdown(f"A critical error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
return critical_error
|
760 |
|
761 |
if __name__ == "__main__":
|
|
|
15 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
16 |
from sentence_transformers import SentenceTransformer
|
17 |
|
18 |
+
# RAG components
|
19 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
20 |
+
from langchain_community.vectorstores import FAISS
|
21 |
from langchain.schema import Document
|
22 |
from langchain.embeddings import HuggingFaceEmbeddings
|
23 |
|
|
|
53 |
is_arabic = len(arabic_chars) > len(text) * 0.5
|
54 |
return "arabic" if is_arabic else "english"
|
55 |
|
56 |
+
# Evaluation metrics
|
57 |
def calculate_bleu(prediction, reference):
|
58 |
"""Calculate BLEU score without any NLTK dependency"""
|
59 |
# Tokenize texts using our own tokenizer
|
|
|
127 |
|
128 |
def evaluate_retrieval_quality(contexts, query, language):
|
129 |
"""Evaluate the quality of retrieved contexts"""
|
130 |
+
# This is a placeholder function
|
131 |
return {
|
132 |
'language_match_ratio': 1.0,
|
133 |
'source_diversity': len(set([ctx.get('source', '') for ctx in contexts])) / max(1, len(contexts)),
|
|
|
207 |
|
208 |
return vector_store
|
209 |
|
210 |
+
# Model Loading and RAG System - Improved to handle SentencePiece issues
|
211 |
@spaces.GPU
|
212 |
def load_model_and_tokenizer():
|
213 |
"""Load the ALLaM-7B model and tokenizer with error handling"""
|
|
|
215 |
print(f"Loading model: {model_name}")
|
216 |
|
217 |
try:
|
218 |
+
# Check if sentencepiece is installed
|
219 |
+
try:
|
220 |
+
import sentencepiece
|
221 |
+
print("SentencePiece is installed")
|
222 |
+
except ImportError:
|
223 |
+
print("Warning: SentencePiece is not installed. Attempting to proceed with AutoTokenizer only.")
|
224 |
+
|
225 |
# First attempt with AutoTokenizer
|
226 |
tokenizer = AutoTokenizer.from_pretrained(
|
227 |
model_name,
|
|
|
238 |
)
|
239 |
|
240 |
print("Model loaded successfully with AutoTokenizer!")
|
241 |
+
return model, tokenizer
|
242 |
|
243 |
except Exception as e:
|
244 |
print(f"First loading attempt failed: {e}")
|
|
|
|
|
|
|
|
|
245 |
|
246 |
+
# If SentencePiece error, provide helpful message
|
247 |
+
if "SentencePiece" in str(e):
|
248 |
+
raise ImportError(
|
249 |
+
"The model requires SentencePiece library which is missing. "
|
250 |
+
"Add 'sentencepiece>=0.1.95' to your requirements.txt file."
|
251 |
+
)
|
|
|
252 |
|
253 |
+
# Other general error
|
254 |
+
raise Exception(f"Failed to load model: {e}")
|
|
|
255 |
|
256 |
def retrieve_context(query, vector_store, top_k=5):
|
257 |
"""Retrieve most relevant document chunks for a given query"""
|
|
|
692 |
print("Files in directory:", os.listdir("."))
|
693 |
print("=" * 50)
|
694 |
|
695 |
+
# Check for SentencePiece
|
696 |
+
try:
|
697 |
+
import sentencepiece
|
698 |
+
print("SentencePiece is installed: ✓")
|
699 |
+
except ImportError:
|
700 |
+
print("WARNING: SentencePiece is NOT installed! This will cause errors with the tokenizer.")
|
701 |
+
|
702 |
# Initialize the system with simplified error handling
|
703 |
try:
|
704 |
# First create a very simple Gradio interface to show we're starting
|
|
|
707 |
gr.Markdown("System is initializing. This may take a few minutes...")
|
708 |
status = gr.Textbox(value="Loading resources...", label="Status")
|
709 |
|
710 |
+
with gr.Blocks(title="Vision 2030 Assistant - Model Loading") as model_interface:
|
711 |
+
gr.Markdown("# Vision 2030 Assistant - Loading Model")
|
712 |
+
gr.Markdown("The system is now loading the ALLaM-7B model. This may take several minutes.")
|
713 |
+
status = gr.Textbox(value="Loading model...", label="Status")
|
714 |
|
715 |
# Now try the actual initialization
|
716 |
try:
|
|
|
722 |
|
723 |
print("Launching interface...")
|
724 |
return interface
|
725 |
+
except ImportError as e:
|
726 |
+
print(f"Import error during initialization: {e}")
|
727 |
+
|
728 |
+
# Create a simple error interface specifically for SentencePiece errors
|
729 |
+
if "SentencePiece" in str(e):
|
730 |
+
with gr.Blocks(title="Vision 2030 Assistant - SentencePiece Error") as sp_error:
|
731 |
+
gr.Markdown("# Vision 2030 Assistant - SentencePiece Error")
|
732 |
+
gr.Markdown("The model requires the SentencePiece library which is missing.")
|
733 |
+
|
734 |
+
gr.Markdown("""
|
735 |
+
## How to Fix:
|
736 |
+
|
737 |
+
Add these lines to your `requirements.txt` file:
|
738 |
+
```
|
739 |
+
sentencepiece>=0.1.95
|
740 |
+
protobuf>=3.20.0
|
741 |
+
```
|
742 |
+
|
743 |
+
Then rebuild your Hugging Face Space.
|
744 |
+
""")
|
745 |
+
|
746 |
+
return sp_error
|
747 |
+
else:
|
748 |
+
# For other import errors
|
749 |
+
with gr.Blocks(title="Vision 2030 Assistant - Import Error") as import_error:
|
750 |
+
gr.Markdown("# Vision 2030 Assistant - Import Error")
|
751 |
+
gr.Markdown(f"An import error occurred: {str(e)}")
|
752 |
+
|
753 |
+
# Display possible solutions
|
754 |
+
gr.Markdown("""
|
755 |
+
## Possible solutions:
|
756 |
+
|
757 |
+
Check your `requirements.txt` file for missing dependencies.
|
758 |
+
""")
|
759 |
+
|
760 |
+
return import_error
|
761 |
except Exception as e:
|
762 |
print(f"Error during initialization: {e}")
|
763 |
import traceback
|
764 |
traceback.print_exc()
|
765 |
|
766 |
+
# Create a general error interface
|
767 |
with gr.Blocks(title="Vision 2030 Assistant - Error") as debug_interface:
|
768 |
gr.Markdown("# Vision 2030 Assistant - Initialization Error")
|
769 |
gr.Markdown("There was an error initializing the assistant.")
|
|
|
804 |
with gr.Blocks(title="Vision 2030 Assistant - Critical Error") as critical_error:
|
805 |
gr.Markdown("# Vision 2030 Assistant - Critical Error")
|
806 |
gr.Markdown(f"A critical error occurred: {str(e)}")
|
807 |
+
|
808 |
+
# Display stacktrace
|
809 |
+
import traceback
|
810 |
+
trace = traceback.format_exc()
|
811 |
+
gr.Textbox(
|
812 |
+
value=trace,
|
813 |
+
label="Error Traceback",
|
814 |
+
lines=15
|
815 |
+
)
|
816 |
return critical_error
|
817 |
|
818 |
if __name__ == "__main__":
|