Spaces:
Runtime error
Runtime error
Calvin commited on
Commit Β·
c0b6253
1
Parent(s): 1543d9b
fix port
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import re
|
|
| 6 |
|
| 7 |
MODEL_ID = "cahya/gpt2-small-indonesian-522M"
|
| 8 |
|
| 9 |
-
# Load tokenizer + model on CPU
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32, device_map={"": "cpu"})
|
| 12 |
|
|
@@ -26,21 +25,18 @@ def generate_ui(prompt):
|
|
| 26 |
cleaned_text = clean_and_extract_json(raw_text)
|
| 27 |
try:
|
| 28 |
parsed = json.loads(cleaned_text)
|
| 29 |
-
# Check fields and strip spaces
|
| 30 |
script = parsed.get("script", "").strip()
|
| 31 |
caption = parsed.get("caption", "").strip()
|
| 32 |
hashtags = parsed.get("hashtags", [])
|
| 33 |
if not script or not caption or not isinstance(hashtags, list):
|
| 34 |
-
raise ValueError("Missing or invalid fields
|
| 35 |
return {
|
| 36 |
"script": script,
|
| 37 |
"caption": caption,
|
| 38 |
"hashtags": hashtags
|
| 39 |
}
|
| 40 |
except Exception:
|
| 41 |
-
# Fallback: don't include the prompt, just first sentence or default text
|
| 42 |
fallback_script = raw_text.strip()
|
| 43 |
-
# Remove prompt repetition if possible
|
| 44 |
if prompt in fallback_script:
|
| 45 |
fallback_script = fallback_script.replace(prompt, "").strip()
|
| 46 |
fallback_caption = fallback_script.split(".")[0].strip() or "Simak info menarik ini!"
|
|
|
|
| 6 |
|
| 7 |
MODEL_ID = "cahya/gpt2-small-indonesian-522M"
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32, device_map={"": "cpu"})
|
| 11 |
|
|
|
|
| 25 |
cleaned_text = clean_and_extract_json(raw_text)
|
| 26 |
try:
|
| 27 |
parsed = json.loads(cleaned_text)
|
|
|
|
| 28 |
script = parsed.get("script", "").strip()
|
| 29 |
caption = parsed.get("caption", "").strip()
|
| 30 |
hashtags = parsed.get("hashtags", [])
|
| 31 |
if not script or not caption or not isinstance(hashtags, list):
|
| 32 |
+
raise ValueError("Missing or invalid fields")
|
| 33 |
return {
|
| 34 |
"script": script,
|
| 35 |
"caption": caption,
|
| 36 |
"hashtags": hashtags
|
| 37 |
}
|
| 38 |
except Exception:
|
|
|
|
| 39 |
fallback_script = raw_text.strip()
|
|
|
|
| 40 |
if prompt in fallback_script:
|
| 41 |
fallback_script = fallback_script.replace(prompt, "").strip()
|
| 42 |
fallback_caption = fallback_script.split(".")[0].strip() or "Simak info menarik ini!"
|