Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,21 +8,23 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
8 |
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
def generate_json(prompt):
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
result = generator(instruction, max_length=256, do_sample=False)
|
13 |
-
generated_text = result[0]["generated_text"]
|
|
|
|
|
14 |
|
15 |
-
# Debug: Print raw model output to inspect
|
16 |
-
print(f"Raw Model Output: {generated_text}")
|
17 |
-
|
18 |
try:
|
19 |
-
# Try parsing the generated text as JSON
|
20 |
parsed = json.loads(generated_text)
|
21 |
formatted_json = json.dumps(parsed, indent=2)
|
22 |
except Exception as e:
|
23 |
-
# If parsing fails, print the error
|
24 |
formatted_json = f"Raw Output:\n{generated_text}\n\nError parsing JSON: {e}"
|
25 |
-
|
26 |
return formatted_json
|
27 |
|
28 |
demo = gr.Interface(
|
|
|
8 |
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
def generate_json(prompt):
|
11 |
+
# Make the instruction explicit: return ONLY JSON, without explanation
|
12 |
+
instruction = (
|
13 |
+
f"Generate only a valid JSON object without any markdown or additional text. "
|
14 |
+
f"The JSON object should have the following keys: title, author, and tags. "
|
15 |
+
f"Fill in dummy values. Now create a JSON object for: {prompt}"
|
16 |
+
)
|
17 |
result = generator(instruction, max_length=256, do_sample=False)
|
18 |
+
generated_text = result[0]["generated_text"].strip() # remove extra whitespace
|
19 |
+
|
20 |
+
print(f"Raw Model Output: {generated_text}") # Debug statement
|
21 |
|
|
|
|
|
|
|
22 |
try:
|
|
|
23 |
parsed = json.loads(generated_text)
|
24 |
formatted_json = json.dumps(parsed, indent=2)
|
25 |
except Exception as e:
|
|
|
26 |
formatted_json = f"Raw Output:\n{generated_text}\n\nError parsing JSON: {e}"
|
27 |
+
|
28 |
return formatted_json
|
29 |
|
30 |
demo = gr.Interface(
|