Update app.py
Browse files
app.py
CHANGED
|
@@ -109,35 +109,43 @@ class SimpleAgent:
|
|
| 109 |
"""Initialize the inference client"""
|
| 110 |
logging.info("Setting up agent...")
|
| 111 |
try:
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
"gpt2",
|
| 116 |
-
"microsoft/DialoGPT-medium",
|
| 117 |
-
"google/flan-t5-base",
|
| 118 |
-
"facebook/blenderbot-400M-distill"
|
| 119 |
-
]
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
logging.info("Agent setup complete.")
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
logging.exception(f"Error during agent setup: {e}")
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def __call__(self, question: str, question_type: str, tools_code: Optional[List[Dict]] = None) -> str:
|
| 143 |
"""Main method to process a question and return an answer"""
|
|
@@ -177,16 +185,40 @@ Response:"""
|
|
| 177 |
logging.info(f"Full question length: {len(question)} characters")
|
| 178 |
logging.info(f"Full prompt length: {len(prompt)} characters")
|
| 179 |
|
| 180 |
-
#
|
| 181 |
try:
|
| 182 |
logging.info("Attempting LLM call...")
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
if not response or response.strip() == "":
|
| 192 |
raise Exception("Empty response from LLM")
|
|
@@ -195,18 +227,9 @@ Response:"""
|
|
| 195 |
logging.error(f"LLM API call failed with error: {str(api_error)}")
|
| 196 |
logging.error(f"Error type: {type(api_error)}")
|
| 197 |
|
| 198 |
-
#
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
# Simple direct prompt without complex parameters
|
| 202 |
-
response = self.client.text_generation(prompt, max_new_tokens=100)
|
| 203 |
-
logging.info(f"Alternative method SUCCESS: {response}")
|
| 204 |
-
except Exception as api_error2:
|
| 205 |
-
logging.error(f"Alternative method also failed: {str(api_error2)}")
|
| 206 |
-
|
| 207 |
-
# Final fallback - but at least try to give a reasonable answer
|
| 208 |
-
response = self._generate_fallback_answer(question, question_type)
|
| 209 |
-
logging.info(f"Using intelligent fallback: {response}")
|
| 210 |
|
| 211 |
|
| 212 |
# Extract the answer from the response
|
|
|
|
| 109 |
"""Initialize the inference client"""
|
| 110 |
logging.info("Setting up agent...")
|
| 111 |
try:
|
| 112 |
+
# Use the HuggingFace pipeline approach instead of InferenceClient
|
| 113 |
+
# This should work more reliably with your token
|
| 114 |
+
logging.info("Trying pipeline approach for text generation...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
try:
|
| 117 |
+
# Try to load a simple text generation pipeline
|
| 118 |
+
self.text_generator = pipeline(
|
| 119 |
+
"text-generation",
|
| 120 |
+
model="gpt2",
|
| 121 |
+
tokenizer="gpt2",
|
| 122 |
+
device=-1 # Use CPU
|
| 123 |
+
)
|
| 124 |
+
self.client = "pipeline" # Flag that we're using pipeline
|
| 125 |
+
logging.info("✅ Successfully initialized with pipeline and gpt2")
|
| 126 |
+
|
| 127 |
+
# Test it
|
| 128 |
+
test_output = self.text_generator("Hello", max_new_tokens=3, do_sample=False)
|
| 129 |
+
logging.info(f"Pipeline test successful: {test_output}")
|
| 130 |
+
|
| 131 |
+
except Exception as pipeline_error:
|
| 132 |
+
logging.warning(f"Pipeline approach failed: {pipeline_error}")
|
| 133 |
+
|
| 134 |
+
# Fallback to InferenceClient with specific working models
|
| 135 |
+
logging.info("Falling back to InferenceClient with simpler approach...")
|
| 136 |
+
|
| 137 |
+
# Try just the basic InferenceClient without specifying model
|
| 138 |
+
self.client = InferenceClient()
|
| 139 |
+
logging.info("✅ Using basic InferenceClient (will handle model selection)")
|
| 140 |
|
| 141 |
logging.info("Agent setup complete.")
|
| 142 |
+
|
| 143 |
except Exception as e:
|
| 144 |
logging.exception(f"Error during agent setup: {e}")
|
| 145 |
+
# Instead of failing completely, set up a basic fallback
|
| 146 |
+
logging.warning("All LLM approaches failed, using fallback mode")
|
| 147 |
+
self.client = "fallback"
|
| 148 |
+
logging.info("Agent setup complete in fallback mode.")
|
| 149 |
|
| 150 |
def __call__(self, question: str, question_type: str, tools_code: Optional[List[Dict]] = None) -> str:
|
| 151 |
"""Main method to process a question and return an answer"""
|
|
|
|
| 185 |
logging.info(f"Full question length: {len(question)} characters")
|
| 186 |
logging.info(f"Full prompt length: {len(prompt)} characters")
|
| 187 |
|
| 188 |
+
# Try different LLM approaches based on what's available
|
| 189 |
try:
|
| 190 |
logging.info("Attempting LLM call...")
|
| 191 |
+
|
| 192 |
+
if self.client == "pipeline":
|
| 193 |
+
# Use the transformers pipeline
|
| 194 |
+
logging.info("Using pipeline approach...")
|
| 195 |
+
outputs = self.text_generator(
|
| 196 |
+
prompt,
|
| 197 |
+
max_new_tokens=100,
|
| 198 |
+
temperature=0.3,
|
| 199 |
+
do_sample=True,
|
| 200 |
+
pad_token_id=50256, # GPT2 pad token
|
| 201 |
+
truncation=True
|
| 202 |
+
)
|
| 203 |
+
response = outputs[0]['generated_text'][len(prompt):].strip()
|
| 204 |
+
logging.info(f"Pipeline SUCCESS - Raw model response: {response}")
|
| 205 |
+
|
| 206 |
+
elif self.client == "fallback":
|
| 207 |
+
# Pure fallback mode - no LLM
|
| 208 |
+
logging.info("Using pure fallback mode...")
|
| 209 |
+
response = self._generate_fallback_answer(question, question_type)
|
| 210 |
+
logging.info(f"Fallback response: {response}")
|
| 211 |
+
|
| 212 |
+
else:
|
| 213 |
+
# InferenceClient approach
|
| 214 |
+
logging.info("Using InferenceClient...")
|
| 215 |
+
response = self.client.text_generation(
|
| 216 |
+
prompt,
|
| 217 |
+
max_new_tokens=200,
|
| 218 |
+
temperature=0.3,
|
| 219 |
+
return_full_text=False
|
| 220 |
+
)
|
| 221 |
+
logging.info(f"InferenceClient SUCCESS - Raw model response: {response}")
|
| 222 |
|
| 223 |
if not response or response.strip() == "":
|
| 224 |
raise Exception("Empty response from LLM")
|
|
|
|
| 227 |
logging.error(f"LLM API call failed with error: {str(api_error)}")
|
| 228 |
logging.error(f"Error type: {type(api_error)}")
|
| 229 |
|
| 230 |
+
# Final fallback
|
| 231 |
+
response = self._generate_fallback_answer(question, question_type)
|
| 232 |
+
logging.info(f"Using intelligent fallback: {response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
# Extract the answer from the response
|