Spaces:
Sleeping
Sleeping
revised app.py
Browse files
app.py
CHANGED
@@ -71,21 +71,22 @@ def load_documents(file_paths):
|
|
71 |
logger.error(f"Error processing file {file_path}: {e}")
|
72 |
return docs
|
73 |
|
74 |
-
# Function to ensure the response ends with
|
75 |
def ensure_complete_sentences(text):
|
76 |
# Use regex to find all complete sentences
|
77 |
sentences = re.findall(r'[^.!?]*[.!?]', text)
|
78 |
if sentences:
|
79 |
-
|
|
|
80 |
return text # Return as is if no complete sentence is found
|
81 |
|
82 |
# Initialize the LLM using ChatGroq with GROQ's API
|
83 |
def initialize_llm(model, temperature, max_tokens):
|
84 |
try:
|
85 |
-
# Allocate
|
86 |
-
|
87 |
-
response_max_tokens = max_tokens -
|
88 |
-
if response_max_tokens <=
|
89 |
raise ValueError("max_tokens is too small to allocate for the response.")
|
90 |
|
91 |
llm = ChatGroq(
|
@@ -129,7 +130,7 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
129 |
custom_prompt_template = PromptTemplate(
|
130 |
input_variables=["context", "question"],
|
131 |
template="""
|
132 |
-
You are an AI assistant with expertise in daily wellness. Your aim is to provide detailed
|
133 |
|
134 |
Context:
|
135 |
{context}
|
@@ -137,7 +138,7 @@ def create_rag_pipeline(file_paths, model, temperature, max_tokens):
|
|
137 |
Question:
|
138 |
{question}
|
139 |
|
140 |
-
Provide a
|
141 |
"""
|
142 |
)
|
143 |
|
@@ -161,7 +162,7 @@ def answer_question(file_paths, model, temperature, max_tokens, question):
|
|
161 |
try:
|
162 |
answer = rag_chain.run(question)
|
163 |
logger.debug("Question answered successfully.")
|
164 |
-
# Post-process to ensure the answer ends with
|
165 |
complete_answer = ensure_complete_sentences(answer)
|
166 |
return complete_answer
|
167 |
except Exception as e:
|
@@ -179,7 +180,7 @@ interface = gr.Interface(
|
|
179 |
inputs=[
|
180 |
gr.Textbox(label="Model Name", value="llama3-8b-8192"),
|
181 |
gr.Slider(label="Temperature", minimum=0, maximum=1, step=0.01, value=0.7),
|
182 |
-
gr.Slider(label="Max Tokens", minimum=
|
183 |
gr.Textbox(label="Question")
|
184 |
],
|
185 |
outputs="text",
|
|
|
71 |
logger.error(f"Error processing file {file_path}: {e}")
|
72 |
return docs
|
73 |
|
74 |
+
# Function to ensure the response ends with complete sentences
|
75 |
def ensure_complete_sentences(text):
|
76 |
# Use regex to find all complete sentences
|
77 |
sentences = re.findall(r'[^.!?]*[.!?]', text)
|
78 |
if sentences:
|
79 |
+
# Join all complete sentences to form the complete answer
|
80 |
+
return ' '.join(sentences).strip()
|
81 |
return text # Return as is if no complete sentence is found
|
82 |
|
83 |
# Initialize the LLM using ChatGroq with GROQ's API
|
84 |
def initialize_llm(model, temperature, max_tokens):
|
85 |
try:
|
86 |
+
# Allocate a portion of tokens for the prompt, e.g., 20%
|
87 |
+
prompt_allocation = int(max_tokens * 0.2)
|
88 |
+
response_max_tokens = max_tokens - prompt_allocation
|
89 |
+
if response_max_tokens <= 50:
|
90 |
raise ValueError("max_tokens is too small to allocate for the response.")
|
91 |
|
92 |
llm = ChatGroq(
|
|
|
130 |
custom_prompt_template = PromptTemplate(
|
131 |
input_variables=["context", "question"],
|
132 |
template="""
|
133 |
+
You are an AI assistant with expertise in daily wellness. Your aim is to provide detailed and comprehensive solutions regarding daily wellness topics without unnecessary verbosity.
|
134 |
|
135 |
Context:
|
136 |
{context}
|
|
|
138 |
Question:
|
139 |
{question}
|
140 |
|
141 |
+
Provide a thorough and complete answer, including relevant examples and a suggested schedule. Ensure that the response does not end abruptly.
|
142 |
"""
|
143 |
)
|
144 |
|
|
|
162 |
try:
|
163 |
answer = rag_chain.run(question)
|
164 |
logger.debug("Question answered successfully.")
|
165 |
+
# Post-process to ensure the answer ends with complete sentences
|
166 |
complete_answer = ensure_complete_sentences(answer)
|
167 |
return complete_answer
|
168 |
except Exception as e:
|
|
|
180 |
inputs=[
|
181 |
gr.Textbox(label="Model Name", value="llama3-8b-8192"),
|
182 |
gr.Slider(label="Temperature", minimum=0, maximum=1, step=0.01, value=0.7),
|
183 |
+
gr.Slider(label="Max Tokens", minimum=200, maximum=1024, step=1, value=500),
|
184 |
gr.Textbox(label="Question")
|
185 |
],
|
186 |
outputs="text",
|