Spaces:
Sleeping
Sleeping
refactor: improve prompt
Browse files
app.py
CHANGED
|
@@ -57,30 +57,37 @@ def load_dataset(query):
|
|
| 57 |
})
|
| 58 |
return df
|
| 59 |
|
| 60 |
-
def generate_answer(question, context, max_length=
|
| 61 |
tokenizer, model = load_models()
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
prompt = f"""Based on scientific research about autism, provide a
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
If the context doesn't contain relevant information about autism, respond with 'I cannot find specific information about this topic in the autism research papers.'
|
| 67 |
|
| 68 |
Question: {question}
|
| 69 |
Context: {context}
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
# Optimize input processing
|
| 74 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=
|
| 75 |
|
| 76 |
with torch.inference_mode():
|
| 77 |
outputs = model.generate(
|
| 78 |
**inputs,
|
| 79 |
max_length=max_length,
|
| 80 |
-
num_beams=
|
| 81 |
-
temperature=0.
|
| 82 |
top_p=0.9,
|
| 83 |
-
repetition_penalty=1.
|
|
|
|
| 84 |
early_stopping=True
|
| 85 |
)
|
| 86 |
|
|
@@ -90,11 +97,13 @@ def generate_answer(question, context, max_length=150):
|
|
| 90 |
if torch.cuda.is_available():
|
| 91 |
torch.cuda.empty_cache()
|
| 92 |
|
| 93 |
-
#
|
| 94 |
if not answer or answer.isspace() or "cannot find" in answer.lower():
|
| 95 |
return "I cannot find specific information about this topic in the autism research papers."
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Streamlit App
|
| 100 |
st.title("🧩 AMA Autism")
|
|
|
|
| 57 |
})
|
| 58 |
return df
|
| 59 |
|
| 60 |
+
def generate_answer(question, context, max_length=300):
|
| 61 |
tokenizer, model = load_models()
|
| 62 |
|
| 63 |
+
# Enhanced prompt for more detailed and structured answers
|
| 64 |
+
prompt = f"""Based on scientific research about autism, provide a comprehensive and structured summary answering the following question.
|
| 65 |
+
Include the following aspects when relevant:
|
| 66 |
+
1. Main findings and conclusions
|
| 67 |
+
2. Supporting evidence or research methods
|
| 68 |
+
3. Clinical implications or practical applications
|
| 69 |
+
4. Any limitations or areas needing further research
|
| 70 |
+
|
| 71 |
+
Use clear headings and bullet points when appropriate to organize the information.
|
| 72 |
If the context doesn't contain relevant information about autism, respond with 'I cannot find specific information about this topic in the autism research papers.'
|
| 73 |
|
| 74 |
Question: {question}
|
| 75 |
Context: {context}
|
| 76 |
|
| 77 |
+
Detailed summary:"""
|
| 78 |
|
| 79 |
# Optimize input processing
|
| 80 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=768)
|
| 81 |
|
| 82 |
with torch.inference_mode():
|
| 83 |
outputs = model.generate(
|
| 84 |
**inputs,
|
| 85 |
max_length=max_length,
|
| 86 |
+
num_beams=4,
|
| 87 |
+
temperature=0.8,
|
| 88 |
top_p=0.9,
|
| 89 |
+
repetition_penalty=1.3,
|
| 90 |
+
length_penalty=1.2,
|
| 91 |
early_stopping=True
|
| 92 |
)
|
| 93 |
|
|
|
|
| 97 |
if torch.cuda.is_available():
|
| 98 |
torch.cuda.empty_cache()
|
| 99 |
|
| 100 |
+
# Enhanced answer validation and formatting
|
| 101 |
if not answer or answer.isspace() or "cannot find" in answer.lower():
|
| 102 |
return "I cannot find specific information about this topic in the autism research papers."
|
| 103 |
|
| 104 |
+
# Format the answer with proper line breaks and structure
|
| 105 |
+
formatted_answer = answer.replace(". ", ".\n").replace("• ", "\n• ")
|
| 106 |
+
return formatted_answer
|
| 107 |
|
| 108 |
# Streamlit App
|
| 109 |
st.title("🧩 AMA Autism")
|