Spaces:
Sleeping
Sleeping
Commit
·
537ecfd
1
Parent(s):
4c33bc8
updated app.py
Browse files
app.py
CHANGED
|
@@ -62,8 +62,25 @@ Include:
|
|
| 62 |
- Example usage
|
| 63 |
- Output demonstration
|
| 64 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
else:
|
| 66 |
-
enhanced_prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Tokenize input
|
| 69 |
inputs = self.tokenizer(
|
|
@@ -80,27 +97,31 @@ Include:
|
|
| 80 |
outputs = self.model.generate(
|
| 81 |
**inputs,
|
| 82 |
max_length=max_length,
|
| 83 |
-
min_length=50, # Reduced minimum length
|
| 84 |
-
temperature=temperature,
|
| 85 |
-
top_p=top_p,
|
| 86 |
do_sample=True,
|
| 87 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 88 |
eos_token_id=self.tokenizer.eos_token_id,
|
| 89 |
-
repetition_penalty=1.
|
| 90 |
no_repeat_ngram_size=3,
|
|
|
|
| 91 |
early_stopping=True,
|
| 92 |
-
num_beams=3, #
|
| 93 |
-
length_penalty=0.
|
| 94 |
)
|
| 95 |
|
| 96 |
# Decode response
|
| 97 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 98 |
|
| 99 |
# Clean up the response
|
| 100 |
if response.startswith(enhanced_prompt):
|
| 101 |
response = response[len(enhanced_prompt):].strip()
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
closures = [
|
| 105 |
"Best regards,",
|
| 106 |
"Sincerely,",
|
|
@@ -117,16 +138,44 @@ Include:
|
|
| 117 |
]
|
| 118 |
|
| 119 |
for closure in closures:
|
| 120 |
-
if
|
| 121 |
-
response = response[
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Ensure code examples are properly formatted
|
| 124 |
if "```python" not in response and "def " in response:
|
| 125 |
response = "```python\n" + response + "\n```"
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
if len(response.strip()) <
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def add_numbers(a, b):
|
| 131 |
'''
|
| 132 |
Add two numbers and return the result
|
|
@@ -144,6 +193,9 @@ num2 = 3
|
|
| 144 |
result = add_numbers(num1, num2)
|
| 145 |
print(f"The sum of {num1} and {num2} is: {result}") # Output: The sum of 5 and 3 is: 8
|
| 146 |
```"""
|
|
|
|
|
|
|
|
|
|
| 147 |
response = fallback_response
|
| 148 |
|
| 149 |
generation_time = time.time() - start_time
|
|
|
|
| 62 |
- Example usage
|
| 63 |
- Output demonstration
|
| 64 |
"""
|
| 65 |
+
elif any(word in prompt.lower() for word in ["explain", "what is", "how does", "describe"]):
|
| 66 |
+
enhanced_prompt = f"""Below is a request for explanation. Please provide a complete, detailed response:
|
| 67 |
+
|
| 68 |
+
{prompt}
|
| 69 |
+
|
| 70 |
+
Your response should include:
|
| 71 |
+
1. A clear explanation in simple terms
|
| 72 |
+
2. Practical examples and applications
|
| 73 |
+
3. Important concepts to understand
|
| 74 |
+
|
| 75 |
+
Response:"""
|
| 76 |
else:
|
| 77 |
+
enhanced_prompt = f"""Below is a request. Please provide a complete, detailed response:
|
| 78 |
+
|
| 79 |
+
{prompt}
|
| 80 |
+
|
| 81 |
+
Response:"""
|
| 82 |
+
|
| 83 |
+
print(f"Enhanced prompt: {enhanced_prompt}") # Debug logging
|
| 84 |
|
| 85 |
# Tokenize input
|
| 86 |
inputs = self.tokenizer(
|
|
|
|
| 97 |
outputs = self.model.generate(
|
| 98 |
**inputs,
|
| 99 |
max_length=max_length,
|
| 100 |
+
min_length=50, # Reduced minimum length requirement
|
| 101 |
+
temperature=max(0.6, temperature), # Ensure minimum temperature
|
| 102 |
+
top_p=min(0.95, top_p), # Cap top_p
|
| 103 |
do_sample=True,
|
| 104 |
pad_token_id=self.tokenizer.pad_token_id,
|
| 105 |
eos_token_id=self.tokenizer.eos_token_id,
|
| 106 |
+
repetition_penalty=1.2, # Increased repetition penalty
|
| 107 |
no_repeat_ngram_size=3,
|
| 108 |
+
num_return_sequences=1,
|
| 109 |
early_stopping=True,
|
| 110 |
+
num_beams=3, # Reduced beam search
|
| 111 |
+
length_penalty=0.7 # Encourage shorter responses
|
| 112 |
)
|
| 113 |
|
| 114 |
# Decode response
|
| 115 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 116 |
+
print(f"Raw response: {response}") # Debug logging
|
| 117 |
|
| 118 |
# Clean up the response
|
| 119 |
if response.startswith(enhanced_prompt):
|
| 120 |
response = response[len(enhanced_prompt):].strip()
|
| 121 |
|
| 122 |
+
print(f"After prompt removal: {response}") # Debug logging
|
| 123 |
+
|
| 124 |
+
# Remove common closure patterns only if they appear at the very end
|
| 125 |
closures = [
|
| 126 |
"Best regards,",
|
| 127 |
"Sincerely,",
|
|
|
|
| 138 |
]
|
| 139 |
|
| 140 |
for closure in closures:
|
| 141 |
+
if response.lower().endswith(closure.lower()):
|
| 142 |
+
response = response[:-(len(closure))].strip()
|
| 143 |
+
|
| 144 |
+
print(f"After closure removal: {response}") # Debug logging
|
| 145 |
|
| 146 |
# Ensure code examples are properly formatted
|
| 147 |
if "```python" not in response and "def " in response:
|
| 148 |
response = "```python\n" + response + "\n```"
|
| 149 |
|
| 150 |
+
# More lenient validation
|
| 151 |
+
if len(response.strip()) < 20 or response.strip() == "Response:": # Only check for very short responses
|
| 152 |
+
print("Response validation failed - using fallback") # Debug logging
|
| 153 |
+
|
| 154 |
+
if "machine learning" in prompt.lower():
|
| 155 |
+
fallback_response = """Machine learning is a branch of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed. Think of it like teaching a child:
|
| 156 |
+
|
| 157 |
+
1. Simple Explanation:
|
| 158 |
+
- Instead of giving strict rules, we show the computer many examples
|
| 159 |
+
- The computer finds patterns in these examples
|
| 160 |
+
- It uses these patterns to make decisions about new situations
|
| 161 |
+
|
| 162 |
+
2. Real-World Applications:
|
| 163 |
+
- Email Spam Detection: Learning to identify unwanted emails based on past examples
|
| 164 |
+
- Netflix Recommendations: Suggesting movies based on what you've watched
|
| 165 |
+
- Face Recognition: Unlocking your phone by learning your facial features
|
| 166 |
+
- Virtual Assistants: Siri and Alexa understanding and responding to voice commands
|
| 167 |
+
- Medical Diagnosis: Helping doctors identify diseases in medical images
|
| 168 |
+
- Fraud Detection: Banks identifying suspicious transactions
|
| 169 |
+
|
| 170 |
+
3. Key Benefits:
|
| 171 |
+
- Automation of complex tasks
|
| 172 |
+
- More accurate predictions over time
|
| 173 |
+
- Ability to handle large amounts of data
|
| 174 |
+
- Continuous improvement through learning
|
| 175 |
+
|
| 176 |
+
Machine learning is transforming industries by automating tasks that once required human intelligence, making processes more efficient and enabling new possibilities in technology."""
|
| 177 |
+
elif "function" in prompt.lower():
|
| 178 |
+
fallback_response = """```python
|
| 179 |
def add_numbers(a, b):
|
| 180 |
'''
|
| 181 |
Add two numbers and return the result
|
|
|
|
| 193 |
result = add_numbers(num1, num2)
|
| 194 |
print(f"The sum of {num1} and {num2} is: {result}") # Output: The sum of 5 and 3 is: 8
|
| 195 |
```"""
|
| 196 |
+
else:
|
| 197 |
+
fallback_response = "I apologize, but I couldn't generate a complete response. Please try adjusting the temperature (try 0.6-0.8) or providing more context in your prompt."
|
| 198 |
+
|
| 199 |
response = fallback_response
|
| 200 |
|
| 201 |
generation_time = time.time() - start_time
|