Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,8 @@ 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.embeddings import HuggingFaceEmbedding
|
| 9 |
from llama_index import ServiceContext
|
| 10 |
from llama_index.llms import HuggingFaceLLM
|
|
@@ -23,10 +24,13 @@ groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
| 23 |
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 24 |
|
| 25 |
# Initialize a local LLM for indexing purposes
|
| 26 |
-
local_llm = HuggingFaceLLM(model_name="gpt2", tokenizer_name="gpt2")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Initialize the index
|
| 32 |
index = None
|
|
@@ -60,11 +64,11 @@ audio_language_dict = {
|
|
| 60 |
def index_text(text: str) -> str:
|
| 61 |
global index
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
if index is None:
|
| 65 |
-
index = VectorStoreIndex.from_documents(
|
| 66 |
else:
|
| 67 |
-
index.insert(
|
| 68 |
return "Text indexed successfully."
|
| 69 |
except Exception as e:
|
| 70 |
logging.error(f"Error in indexing: {str(e)}")
|
|
@@ -82,6 +86,11 @@ def chat_with_context(question: str, model: str) -> str:
|
|
| 82 |
)
|
| 83 |
context = query_engine.query(question).response
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
| 86 |
|
| 87 |
chat_completion = groq_client.chat.completions.create(
|
|
@@ -92,12 +101,14 @@ def chat_with_context(question: str, model: str) -> str:
|
|
| 92 |
}
|
| 93 |
],
|
| 94 |
model=model,
|
|
|
|
| 95 |
)
|
| 96 |
return chat_completion.choices[0].message.content
|
| 97 |
except Exception as e:
|
| 98 |
logging.error(f"Error in chat: {str(e)}")
|
| 99 |
return f"Error in chat: {str(e)}"
|
| 100 |
|
|
|
|
| 101 |
# Translation function
|
| 102 |
def translate_text(text, target_lang_code):
|
| 103 |
try:
|
|
|
|
| 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.embeddings import HuggingFaceEmbedding
|
| 10 |
from llama_index import ServiceContext
|
| 11 |
from llama_index.llms import HuggingFaceLLM
|
|
|
|
| 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=512, max_new_tokens=256)
|
| 28 |
|
| 29 |
+
# Set up node parser for chunking
|
| 30 |
+
node_parser = SimpleNodeParser.from_defaults(chunk_size=256, 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)
|
| 34 |
|
| 35 |
# Initialize the index
|
| 36 |
index = None
|
|
|
|
| 64 |
def index_text(text: str) -> str:
|
| 65 |
global index
|
| 66 |
try:
|
| 67 |
+
documents = [Document(text=text)]
|
| 68 |
if index is None:
|
| 69 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 70 |
else:
|
| 71 |
+
index.insert(documents[0])
|
| 72 |
return "Text indexed successfully."
|
| 73 |
except Exception as e:
|
| 74 |
logging.error(f"Error in indexing: {str(e)}")
|
|
|
|
| 86 |
)
|
| 87 |
context = query_engine.query(question).response
|
| 88 |
|
| 89 |
+
# Truncate context if it's too long
|
| 90 |
+
max_context_length = 2048 # Adjust as needed
|
| 91 |
+
if len(context) > max_context_length:
|
| 92 |
+
context = context[:max_context_length] + "..."
|
| 93 |
+
|
| 94 |
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
| 95 |
|
| 96 |
chat_completion = groq_client.chat.completions.create(
|
|
|
|
| 101 |
}
|
| 102 |
],
|
| 103 |
model=model,
|
| 104 |
+
max_tokens=500 # Limit the response length
|
| 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:
|