Spaces:
Sleeping
Sleeping
refactor: improve response
Browse files
app.py
CHANGED
|
@@ -167,24 +167,21 @@ def generate_answer(question, context, max_length=512):
|
|
| 167 |
if model is None or tokenizer is None:
|
| 168 |
return "Error: Could not load the model. Please try again later."
|
| 169 |
|
| 170 |
-
if not context or len(context.strip()) == 0:
|
| 171 |
-
return "No relevant autism research papers found to answer your question. Please try rephrasing your question."
|
| 172 |
-
|
| 173 |
# Format the context as a structured query
|
| 174 |
-
prompt = f"""
|
| 175 |
-
|
| 176 |
-
Research Context:
|
| 177 |
-
{context}
|
| 178 |
|
| 179 |
Question: {question}
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
If the research papers don't directly address
|
| 188 |
|
| 189 |
try:
|
| 190 |
# Generate response
|
|
@@ -194,7 +191,7 @@ If the research papers don't directly address autism and the question, explain w
|
|
| 194 |
outputs = model.generate(
|
| 195 |
**inputs,
|
| 196 |
max_length=max_length,
|
| 197 |
-
min_length=
|
| 198 |
num_beams=4,
|
| 199 |
length_penalty=1.5,
|
| 200 |
temperature=0.7,
|
|
@@ -204,15 +201,29 @@ If the research papers don't directly address autism and the question, explain w
|
|
| 204 |
|
| 205 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 206 |
|
| 207 |
-
# If response is too short or empty, provide a
|
| 208 |
-
if len(response.strip()) <
|
| 209 |
-
return """
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# Format the response for better readability
|
| 218 |
formatted_response = response.replace(". ", ".\n").replace("• ", "\n• ")
|
|
@@ -231,7 +242,7 @@ This app searches through scientific papers to answer your questions about autis
|
|
| 231 |
For best results, be specific in your questions.
|
| 232 |
""")
|
| 233 |
|
| 234 |
-
query = st.text_input("Please ask me anything about autism
|
| 235 |
|
| 236 |
if query:
|
| 237 |
with st.status("Searching for answers...") as status:
|
|
|
|
| 167 |
if model is None or tokenizer is None:
|
| 168 |
return "Error: Could not load the model. Please try again later."
|
| 169 |
|
|
|
|
|
|
|
|
|
|
| 170 |
# Format the context as a structured query
|
| 171 |
+
prompt = f"""You are an expert in autism research. Provide a comprehensive answer about autism, incorporating both general knowledge and specific research findings when available.
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
Question: {question}
|
| 174 |
|
| 175 |
+
Recent Research Context:
|
| 176 |
+
{context}
|
| 177 |
+
|
| 178 |
+
Instructions: Provide a detailed response that:
|
| 179 |
+
1. Starts with a general overview of the topic as it relates to autism
|
| 180 |
+
2. Incorporates specific findings from the provided research papers when relevant
|
| 181 |
+
3. Discusses practical implications for individuals with autism and their families
|
| 182 |
+
4. Mentions any limitations in current understanding
|
| 183 |
|
| 184 |
+
If the research papers don't directly address the question, focus on providing general, well-established information about autism while noting what specific research would be helpful."""
|
| 185 |
|
| 186 |
try:
|
| 187 |
# Generate response
|
|
|
|
| 191 |
outputs = model.generate(
|
| 192 |
**inputs,
|
| 193 |
max_length=max_length,
|
| 194 |
+
min_length=150, # Increased minimum length for more comprehensive answers
|
| 195 |
num_beams=4,
|
| 196 |
length_penalty=1.5,
|
| 197 |
temperature=0.7,
|
|
|
|
| 201 |
|
| 202 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 203 |
|
| 204 |
+
# If response is too short or empty, provide a general overview
|
| 205 |
+
if len(response.strip()) < 100:
|
| 206 |
+
return f"""Here's what we know about autism in relation to your question about {question}:
|
| 207 |
+
|
| 208 |
+
1. General Understanding:
|
| 209 |
+
- Autism Spectrum Disorder (ASD) is a complex developmental condition
|
| 210 |
+
- It affects how a person communicates, learns, and interacts with others
|
| 211 |
+
- Each person with autism has unique strengths and challenges
|
| 212 |
+
|
| 213 |
+
2. Key Aspects:
|
| 214 |
+
- Communication and social interaction
|
| 215 |
+
- Repetitive behaviors and specific interests
|
| 216 |
+
- Sensory sensitivities
|
| 217 |
+
- Early intervention is important
|
| 218 |
+
|
| 219 |
+
3. Current Research:
|
| 220 |
+
While the provided research papers don't directly address your specific question, researchers are actively studying various aspects of autism to better understand its causes, characteristics, and effective interventions.
|
| 221 |
+
|
| 222 |
+
For more specific information, try asking about:
|
| 223 |
+
- Specific symptoms or characteristics
|
| 224 |
+
- Diagnostic processes
|
| 225 |
+
- Treatment approaches
|
| 226 |
+
- Current research in specific areas"""
|
| 227 |
|
| 228 |
# Format the response for better readability
|
| 229 |
formatted_response = response.replace(". ", ".\n").replace("• ", "\n• ")
|
|
|
|
| 242 |
For best results, be specific in your questions.
|
| 243 |
""")
|
| 244 |
|
| 245 |
+
query = st.text_input("Please ask me anything about autism ✨")
|
| 246 |
|
| 247 |
if query:
|
| 248 |
with st.status("Searching for answers...") as status:
|