Spaces:
Sleeping
Sleeping
refactor: improve response
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ DATA_DIR = "/data" if os.path.exists("/data") else "."
|
|
17 |
DATASET_DIR = os.path.join(DATA_DIR, "rag_dataset")
|
18 |
DATASET_PATH = os.path.join(DATASET_DIR, "dataset")
|
19 |
TOKENIZER_MODEL = "google/flan-t5-small"
|
20 |
-
SUMMARIZATION_MODEL= "
|
21 |
|
22 |
@st.cache_resource
|
23 |
def load_local_model():
|
@@ -209,44 +209,47 @@ def generate_answer(question, context, max_length=512):
|
|
209 |
clean_question = clean_text(question)
|
210 |
|
211 |
# Format the input for T5 (it expects a specific format)
|
212 |
-
input_text = f"""
|
|
|
213 |
|
214 |
Question: {clean_question}
|
215 |
|
216 |
-
Research Papers:
|
217 |
{clean_context}
|
218 |
|
219 |
-
Instructions:
|
220 |
-
1. Begin with a clear and concise overview of autism, explaining its key characteristics and significance.
|
221 |
-
2. Use the research papers to support your explanation, citing them in the format: "According to [PAPER TITLE], ...".
|
222 |
-
3. Integrate findings from the papers naturally into your response, ensuring the information is accurate and relevant.
|
223 |
-
4. Focus on providing informative, helpful, and easy-to-understand insights.
|
224 |
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
try:
|
228 |
# T5 expects a specific format for the input
|
229 |
inputs = tokenizer(input_text,
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
with torch.inference_mode():
|
236 |
outputs = model.generate(
|
237 |
**inputs,
|
238 |
max_length=max_length,
|
239 |
min_length=200,
|
240 |
-
num_beams=
|
241 |
-
length_penalty=1.
|
242 |
-
temperature=0.
|
243 |
repetition_penalty=1.2,
|
244 |
early_stopping=True,
|
245 |
-
no_repeat_ngram_size=
|
246 |
do_sample=True,
|
247 |
-
top_k=
|
248 |
-
top_p=0.
|
249 |
)
|
|
|
250 |
|
251 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
252 |
response = clean_text(response)
|
|
|
17 |
DATASET_DIR = os.path.join(DATA_DIR, "rag_dataset")
|
18 |
DATASET_PATH = os.path.join(DATASET_DIR, "dataset")
|
19 |
TOKENIZER_MODEL = "google/flan-t5-small"
|
20 |
+
SUMMARIZATION_MODEL= "tiiuae/falcon-40b"
|
21 |
|
22 |
@st.cache_resource
|
23 |
def load_local_model():
|
|
|
209 |
clean_question = clean_text(question)
|
210 |
|
211 |
# Format the input for T5 (it expects a specific format)
|
212 |
+
input_text = f"""Objective:
|
213 |
+
Generate a clear, informative, and well-structured answer about autism, making the content easy to understand for a general audience. Use the provided research papers to support your explanations.
|
214 |
|
215 |
Question: {clean_question}
|
216 |
|
217 |
+
Research Papers:
|
218 |
{clean_context}
|
219 |
|
220 |
+
Instructions:
|
|
|
|
|
|
|
|
|
221 |
|
222 |
+
Start with a simple explanation – Clearly define what autism is in an easy-to-understand way, avoiding overly complex technical terms.
|
223 |
+
Use real-life examples – Whenever possible, include practical examples to illustrate key concepts.
|
224 |
+
Cite research in an accessible way – Instead of just referencing papers, explain their findings in a way that anyone can understand. Example: "A study from X University found that..."
|
225 |
+
Avoid scientific jargon – If a technical term is necessary, provide a simple explanation.
|
226 |
+
Organize the response into sections – Use lists and short paragraphs to improve readability.
|
227 |
+
Write your answer in a friendly and accessible tone, ensuring that anyone, regardless of their background knowledge, can understand the information provided."""
|
228 |
|
229 |
try:
|
230 |
# T5 expects a specific format for the input
|
231 |
inputs = tokenizer(input_text,
|
232 |
+
return_tensors="pt",
|
233 |
+
max_length=1024,
|
234 |
+
truncation=True,
|
235 |
+
padding=True)
|
236 |
+
|
237 |
with torch.inference_mode():
|
238 |
outputs = model.generate(
|
239 |
**inputs,
|
240 |
max_length=max_length,
|
241 |
min_length=200,
|
242 |
+
num_beams=3, # Reduzindo para mais variedade
|
243 |
+
length_penalty=1.2, # Melhor equilíbrio entre concisão e detalhes
|
244 |
+
temperature=0.8, # Aumentando um pouco para mais fluidez
|
245 |
repetition_penalty=1.2,
|
246 |
early_stopping=True,
|
247 |
+
no_repeat_ngram_size=2, # Mantendo variação no texto
|
248 |
do_sample=True,
|
249 |
+
top_k=30, # Reduzindo para respostas mais coerentes
|
250 |
+
top_p=0.9 # Equilibrando diversidade e precisão
|
251 |
)
|
252 |
+
|
253 |
|
254 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
255 |
response = clean_text(response)
|