Spaces:
Sleeping
Sleeping
ming
commited on
Commit
·
4c4036e
1
Parent(s):
9452571
Fix Outlines API usage - handle different calling patterns
Browse files- Try json_schema(schema) then call with model
- Fallback to json_schema(model, schema)
- Handle different generator patterns (.stream() vs direct call)
- Fixes 'takes 1 positional argument but 2 were given' error
app/services/structured_summarizer.py
CHANGED
|
@@ -292,8 +292,25 @@ class StructuredSummarizer:
|
|
| 292 |
# Also warm up Outlines JSON generation
|
| 293 |
if OUTLINES_AVAILABLE and self.outlines_model is not None:
|
| 294 |
try:
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
logger.info("✅ V4 Outlines JSON warmup successful")
|
| 298 |
except Exception as e:
|
| 299 |
logger.warning(f"⚠️ V4 Outlines JSON warmup failed: {e}")
|
|
@@ -1000,12 +1017,31 @@ Rules:
|
|
| 1000 |
return
|
| 1001 |
|
| 1002 |
# Create an Outlines generator bound to the StructuredSummary schema
|
| 1003 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1004 |
|
| 1005 |
start_time = time.time()
|
| 1006 |
|
| 1007 |
# Stream tokens; each token is a string fragment of the final JSON object
|
| 1008 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1009 |
# Each `token` is a raw string fragment; just pass it through
|
| 1010 |
if token:
|
| 1011 |
yield token
|
|
|
|
| 292 |
# Also warm up Outlines JSON generation
|
| 293 |
if OUTLINES_AVAILABLE and self.outlines_model is not None:
|
| 294 |
try:
|
| 295 |
+
# Try the same pattern as in the streaming method
|
| 296 |
+
try:
|
| 297 |
+
json_gen_func = outlines_generate_json(StructuredSummary)
|
| 298 |
+
dummy_gen = json_gen_func(self.outlines_model)
|
| 299 |
+
except TypeError:
|
| 300 |
+
dummy_gen = outlines_generate_json(self.outlines_model, StructuredSummary)
|
| 301 |
+
|
| 302 |
+
# Try to call it
|
| 303 |
+
try:
|
| 304 |
+
result = dummy_gen("Warmup text for Outlines structured summary.")
|
| 305 |
+
# Consume the generator if it's a generator
|
| 306 |
+
if hasattr(result, '__iter__'):
|
| 307 |
+
_ = list(result)[:1] # Just consume first item for warmup
|
| 308 |
+
except TypeError:
|
| 309 |
+
# Maybe it needs prompt as keyword arg or different pattern
|
| 310 |
+
result = dummy_gen.stream("Warmup text") if hasattr(dummy_gen, 'stream') else dummy_gen("Warmup")
|
| 311 |
+
if hasattr(result, '__iter__'):
|
| 312 |
+
_ = list(result)[:1]
|
| 313 |
+
|
| 314 |
logger.info("✅ V4 Outlines JSON warmup successful")
|
| 315 |
except Exception as e:
|
| 316 |
logger.warning(f"⚠️ V4 Outlines JSON warmup failed: {e}")
|
|
|
|
| 1017 |
return
|
| 1018 |
|
| 1019 |
# Create an Outlines generator bound to the StructuredSummary schema
|
| 1020 |
+
# In version 0.0.34, the API might be different - try different patterns
|
| 1021 |
+
try:
|
| 1022 |
+
# Pattern 1: json_schema(schema) returns a function that takes model and prompt
|
| 1023 |
+
json_generator_func = outlines_generate_json(StructuredSummary)
|
| 1024 |
+
json_generator = json_generator_func(self.outlines_model)
|
| 1025 |
+
except TypeError:
|
| 1026 |
+
# Pattern 2: json_schema(model, schema) - original pattern
|
| 1027 |
+
json_generator = outlines_generate_json(self.outlines_model, StructuredSummary)
|
| 1028 |
|
| 1029 |
start_time = time.time()
|
| 1030 |
|
| 1031 |
# Stream tokens; each token is a string fragment of the final JSON object
|
| 1032 |
+
# The generator might have .stream() method or be directly iterable
|
| 1033 |
+
try:
|
| 1034 |
+
# Try .stream() method first
|
| 1035 |
+
token_iter = json_generator.stream(prompt)
|
| 1036 |
+
except AttributeError:
|
| 1037 |
+
# If no .stream(), try calling it directly with prompt
|
| 1038 |
+
try:
|
| 1039 |
+
token_iter = json_generator(prompt)
|
| 1040 |
+
except TypeError:
|
| 1041 |
+
# Last resort: maybe it's a generator factory that needs model and prompt
|
| 1042 |
+
token_iter = json_generator(self.outlines_model, prompt)
|
| 1043 |
+
|
| 1044 |
+
for token in token_iter:
|
| 1045 |
# Each `token` is a raw string fragment; just pass it through
|
| 1046 |
if token:
|
| 1047 |
yield token
|