Update app.py
Browse files
app.py
CHANGED
|
@@ -4,39 +4,42 @@ import os
|
|
| 4 |
from gtts import gTTS
|
| 5 |
from deep_translator import GoogleTranslator
|
| 6 |
import logging
|
| 7 |
-
from llama_index import VectorStoreIndex, Document
|
|
|
|
| 8 |
from llama_index.llms import HuggingFaceLLM
|
| 9 |
-
from llama_index import ServiceContext
|
| 10 |
import torch
|
| 11 |
|
| 12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
|
| 13 |
|
| 14 |
-
# Initialize the LLM
|
| 15 |
try:
|
| 16 |
llm = HuggingFaceLLM(
|
| 17 |
-
context_window=1024
|
| 18 |
max_new_tokens=256,
|
| 19 |
generate_kwargs={"temperature": 0.7, "do_sample": False},
|
| 20 |
tokenizer_name="gpt2",
|
| 21 |
model_name="gpt2",
|
| 22 |
device_map="auto",
|
| 23 |
-
tokenizer_kwargs={"max_length":
|
| 24 |
model_kwargs={"torch_dtype": torch.float32},
|
| 25 |
)
|
| 26 |
except ImportError:
|
| 27 |
# Fallback if Accelerate is not available
|
| 28 |
llm = HuggingFaceLLM(
|
| 29 |
-
context_window=1024
|
| 30 |
max_new_tokens=256,
|
| 31 |
generate_kwargs={"temperature": 0.7, "do_sample": False},
|
| 32 |
tokenizer_name="gpt2",
|
| 33 |
model_name="gpt2",
|
| 34 |
-
tokenizer_kwargs={"max_length":
|
| 35 |
model_kwargs={"torch_dtype": torch.float32},
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Initialize the ServiceContext
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Initialize the index
|
| 42 |
index = None
|
|
@@ -70,13 +73,14 @@ audio_language_dict = {
|
|
| 70 |
def index_text(text: str) -> str:
|
| 71 |
global index
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
if index is None:
|
| 75 |
-
index = VectorStoreIndex.from_documents(
|
| 76 |
else:
|
| 77 |
-
index.insert(
|
| 78 |
return "Text indexed successfully."
|
| 79 |
except Exception as e:
|
|
|
|
| 80 |
return f"Error indexing text: {str(e)}"
|
| 81 |
|
| 82 |
def chat_with_context(question: str) -> str:
|
|
@@ -85,10 +89,14 @@ def chat_with_context(question: str) -> str:
|
|
| 85 |
return "Please index some text first."
|
| 86 |
|
| 87 |
try:
|
| 88 |
-
query_engine = index.as_query_engine(
|
|
|
|
|
|
|
|
|
|
| 89 |
response = query_engine.query(question)
|
| 90 |
return str(response)
|
| 91 |
except Exception as e:
|
|
|
|
| 92 |
return f"Error in chat: {str(e)}"
|
| 93 |
|
| 94 |
# Translation function
|
|
|
|
| 4 |
from gtts import gTTS
|
| 5 |
from deep_translator import GoogleTranslator
|
| 6 |
import logging
|
| 7 |
+
from llama_index import VectorStoreIndex, Document, SimpleDirectoryReader
|
| 8 |
+
from llama_index.node_parser import SimpleNodeParser
|
| 9 |
from llama_index.llms import HuggingFaceLLM
|
| 10 |
+
from llama_index import ServiceContext, set_global_service_context
|
| 11 |
import torch
|
| 12 |
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
|
| 14 |
|
| 15 |
+
# Initialize the LLM with a smaller context window
|
| 16 |
try:
|
| 17 |
llm = HuggingFaceLLM(
|
| 18 |
+
context_window=512, # Reduced from 1024
|
| 19 |
max_new_tokens=256,
|
| 20 |
generate_kwargs={"temperature": 0.7, "do_sample": False},
|
| 21 |
tokenizer_name="gpt2",
|
| 22 |
model_name="gpt2",
|
| 23 |
device_map="auto",
|
| 24 |
+
tokenizer_kwargs={"max_length": 512}, # Reduced from 1024
|
| 25 |
model_kwargs={"torch_dtype": torch.float32},
|
| 26 |
)
|
| 27 |
except ImportError:
|
| 28 |
# Fallback if Accelerate is not available
|
| 29 |
llm = HuggingFaceLLM(
|
| 30 |
+
context_window=512, # Reduced from 1024
|
| 31 |
max_new_tokens=256,
|
| 32 |
generate_kwargs={"temperature": 0.7, "do_sample": False},
|
| 33 |
tokenizer_name="gpt2",
|
| 34 |
model_name="gpt2",
|
| 35 |
+
tokenizer_kwargs={"max_length": 512}, # Reduced from 1024
|
| 36 |
model_kwargs={"torch_dtype": torch.float32},
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Initialize the ServiceContext with a chunk size
|
| 40 |
+
node_parser = SimpleNodeParser.from_defaults(chunk_size=256) # Adjust chunk size as needed
|
| 41 |
+
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local", node_parser=node_parser)
|
| 42 |
+
set_global_service_context(service_context)
|
| 43 |
|
| 44 |
# Initialize the index
|
| 45 |
index = None
|
|
|
|
| 73 |
def index_text(text: str) -> str:
|
| 74 |
global index
|
| 75 |
try:
|
| 76 |
+
documents = [Document(text=text)]
|
| 77 |
if index is None:
|
| 78 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 79 |
else:
|
| 80 |
+
index.insert(documents[0])
|
| 81 |
return "Text indexed successfully."
|
| 82 |
except Exception as e:
|
| 83 |
+
logging.error(f"Error in indexing: {str(e)}")
|
| 84 |
return f"Error indexing text: {str(e)}"
|
| 85 |
|
| 86 |
def chat_with_context(question: str) -> str:
|
|
|
|
| 89 |
return "Please index some text first."
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
query_engine = index.as_query_engine(
|
| 93 |
+
similarity_top_k=2, # Adjust as needed
|
| 94 |
+
response_mode="compact"
|
| 95 |
+
)
|
| 96 |
response = query_engine.query(question)
|
| 97 |
return str(response)
|
| 98 |
except Exception as e:
|
| 99 |
+
logging.error(f"Error in chat: {str(e)}")
|
| 100 |
return f"Error in chat: {str(e)}"
|
| 101 |
|
| 102 |
# Translation function
|