Update app.py
Browse files
app.py
CHANGED
|
@@ -23,11 +23,11 @@ groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
| 23 |
# Initialize the embedding model
|
| 24 |
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
|
| 26 |
-
# Initialize a local LLM for indexing purposes
|
| 27 |
-
local_llm = HuggingFaceLLM(model_name="gpt2", tokenizer_name="gpt2", context_window=
|
| 28 |
|
| 29 |
-
# Set up node parser for chunking
|
| 30 |
-
node_parser = SimpleNodeParser.from_defaults(chunk_size=
|
| 31 |
|
| 32 |
# Initialize the ServiceContext with the local LLM and node parser
|
| 33 |
service_context = ServiceContext.from_defaults(llm=local_llm, embed_model=embed_model, node_parser=node_parser)
|
|
@@ -61,7 +61,7 @@ audio_language_dict = {
|
|
| 61 |
"Malayalam": {"code": "ml"}
|
| 62 |
}
|
| 63 |
|
| 64 |
-
|
| 65 |
global index
|
| 66 |
try:
|
| 67 |
documents = [Document(text=text)]
|
|
@@ -81,13 +81,13 @@ def chat_with_context(question: str, model: str) -> str:
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
query_engine = index.as_query_engine(
|
| 84 |
-
similarity_top_k=
|
| 85 |
response_mode="compact"
|
| 86 |
)
|
| 87 |
context = query_engine.query(question).response
|
| 88 |
|
| 89 |
# Truncate context if it's too long
|
| 90 |
-
max_context_length =
|
| 91 |
if len(context) > max_context_length:
|
| 92 |
context = context[:max_context_length] + "..."
|
| 93 |
|
|
@@ -101,14 +101,13 @@ def chat_with_context(question: str, model: str) -> str:
|
|
| 101 |
}
|
| 102 |
],
|
| 103 |
model=model,
|
| 104 |
-
max_tokens=
|
| 105 |
)
|
| 106 |
return chat_completion.choices[0].message.content
|
| 107 |
except Exception as e:
|
| 108 |
logging.error(f"Error in chat: {str(e)}")
|
| 109 |
return f"Error in chat: {str(e)}"
|
| 110 |
|
| 111 |
-
|
| 112 |
# Translation function
|
| 113 |
def translate_text(text, target_lang_code):
|
| 114 |
try:
|
|
|
|
| 23 |
# Initialize the embedding model
|
| 24 |
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
|
| 26 |
+
# Initialize a local LLM for indexing purposes with reduced context window
|
| 27 |
+
local_llm = HuggingFaceLLM(model_name="gpt2", tokenizer_name="gpt2", context_window=256, max_new_tokens=128)
|
| 28 |
|
| 29 |
+
# Set up node parser for chunking with smaller chunk size
|
| 30 |
+
node_parser = SimpleNodeParser.from_defaults(chunk_size=128, chunk_overlap=20)
|
| 31 |
|
| 32 |
# Initialize the ServiceContext with the local LLM and node parser
|
| 33 |
service_context = ServiceContext.from_defaults(llm=local_llm, embed_model=embed_model, node_parser=node_parser)
|
|
|
|
| 61 |
"Malayalam": {"code": "ml"}
|
| 62 |
}
|
| 63 |
|
| 64 |
+
ef index_text(text: str) -> str:
|
| 65 |
global index
|
| 66 |
try:
|
| 67 |
documents = [Document(text=text)]
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
query_engine = index.as_query_engine(
|
| 84 |
+
similarity_top_k=1,
|
| 85 |
response_mode="compact"
|
| 86 |
)
|
| 87 |
context = query_engine.query(question).response
|
| 88 |
|
| 89 |
# Truncate context if it's too long
|
| 90 |
+
max_context_length = 1024 # Reduced from 2048
|
| 91 |
if len(context) > max_context_length:
|
| 92 |
context = context[:max_context_length] + "..."
|
| 93 |
|
|
|
|
| 101 |
}
|
| 102 |
],
|
| 103 |
model=model,
|
| 104 |
+
max_tokens=256 # Reduced from 500
|
| 105 |
)
|
| 106 |
return chat_completion.choices[0].message.content
|
| 107 |
except Exception as e:
|
| 108 |
logging.error(f"Error in chat: {str(e)}")
|
| 109 |
return f"Error in chat: {str(e)}"
|
| 110 |
|
|
|
|
| 111 |
# Translation function
|
| 112 |
def translate_text(text, target_lang_code):
|
| 113 |
try:
|