Spaces:
Runtime error
Runtime error
Calvin commited on
Commit Β·
8498878
1
Parent(s): 51cae58
change model
Browse files
app.py
CHANGED
|
@@ -2,41 +2,65 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
MODEL_ID = "cahya/gpt2-small-indonesian-522M"
|
| 7 |
|
| 8 |
-
# Load tokenizer + model on CPU
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32, device_map={"": "cpu"})
|
| 11 |
|
| 12 |
-
generator = pipeline(
|
| 13 |
-
"text-generation",
|
| 14 |
-
model=model,
|
| 15 |
-
tokenizer=tokenizer
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
-
parsed = json.loads(
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception:
|
|
|
|
|
|
|
| 27 |
return {
|
| 28 |
-
"script":
|
| 29 |
-
"caption":
|
| 30 |
"hashtags": ["#indonesia", "#fyp"]
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
title="TikTok Script Generator",
|
| 38 |
-
description="Generates short Indonesian scripts (JSON)."
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
demo.launch(server_name="0.0.0.0",
|
|
|
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import json
|
| 5 |
+
import re
|
| 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 |
|
| 12 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def clean_and_extract_json(text):
|
| 15 |
+
json_start = text.find('{')
|
| 16 |
+
if json_start >= 0:
|
| 17 |
+
text = text[json_start:]
|
| 18 |
+
text = re.sub(r"```json|```", "", text).strip()
|
| 19 |
+
return text
|
| 20 |
|
| 21 |
+
# This function is used in the UI
|
| 22 |
+
def generate_ui(prompt):
|
| 23 |
+
out = generator(prompt, max_new_tokens=180, do_sample=True, top_p=0.95, top_k=40, temperature=0.8)
|
| 24 |
+
raw_text = out[0]["generated_text"]
|
| 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 fields")
|
| 33 |
+
return {
|
| 34 |
+
"script": script,
|
| 35 |
+
"caption": caption,
|
| 36 |
+
"hashtags": hashtags
|
| 37 |
+
}
|
| 38 |
except Exception:
|
| 39 |
+
fallback_script = raw_text.strip()
|
| 40 |
+
fallback_caption = fallback_script.split(".")[0].strip() or "Simak info menarik ini!"
|
| 41 |
return {
|
| 42 |
+
"script": fallback_script,
|
| 43 |
+
"caption": fallback_caption,
|
| 44 |
"hashtags": ["#indonesia", "#fyp"]
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# This is the API function Gradio will call, input/output signature must match components exactly
|
| 48 |
+
@gr.api(
|
| 49 |
+
input=gr.Textbox(lines=6),
|
| 50 |
+
output=gr.JSON()
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
+
def generate_api(prompt):
|
| 53 |
+
return generate_ui(prompt)
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
prompt_input = gr.Textbox(lines=6, placeholder="Prompt...")
|
| 57 |
+
output_json = gr.JSON()
|
| 58 |
+
|
| 59 |
+
generate_btn = gr.Button("Generate")
|
| 60 |
+
generate_btn.click(fn=generate_ui, inputs=prompt_input, outputs=output_json)
|
| 61 |
+
|
| 62 |
+
gr.Markdown("### TikTok Script Generator")
|
| 63 |
+
gr.Markdown("Generates short Indonesian scripts (JSON).")
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
demo.launch(server_name="0.0.0.0", port=7860, enable_api=True)
|