Calvin commited on
Commit
2bf8ad3
Β·
1 Parent(s): 511401d

return json

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -1,11 +1,11 @@
1
- # app.py
2
  import gradio as gr
3
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
4
  import torch
 
5
 
6
- MODEL_ID = "cahya/gpt2-small-indonesian-522M" # change if needed
7
 
8
- # Load tokenizer + model (CPU)
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
10
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32, device_map={"": "cpu"})
11
 
@@ -16,16 +16,24 @@ generator = pipeline(
16
  )
17
 
18
  def generate(prompt):
19
- # prompt already instructs JSON-only output; but we keep generation stable
20
  out = generator(prompt, max_new_tokens=180, do_sample=True, top_p=0.95, top_k=40, temperature=0.8)
21
  text = out[0]["generated_text"]
22
- return text
23
 
24
- # Simple gradio UI (the same function is exposed as API)
 
 
 
 
 
 
 
 
 
 
25
  demo = gr.Interface(
26
  fn=generate,
27
  inputs=gr.Textbox(lines=6, placeholder="Prompt..."),
28
- outputs=gr.Textbox(),
29
  title="TikTok Script Generator",
30
  description="Generates short Indonesian scripts (JSON)."
31
  )
 
 
1
  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
 
 
16
  )
17
 
18
  def generate(prompt):
 
19
  out = generator(prompt, max_new_tokens=180, do_sample=True, top_p=0.95, top_k=40, temperature=0.8)
20
  text = out[0]["generated_text"]
 
21
 
22
+ # Try to parse the output as JSON, fallback to raw text
23
+ try:
24
+ parsed = json.loads(text)
25
+ return parsed
26
+ except Exception:
27
+ return {
28
+ "script": text.strip(),
29
+ "caption": "Generated caption",
30
+ "hashtags": ["#indonesia", "#fyp"]
31
+ }
32
+
33
  demo = gr.Interface(
34
  fn=generate,
35
  inputs=gr.Textbox(lines=6, placeholder="Prompt..."),
36
+ outputs=gr.JSON(), # JSON output for proper API response
37
  title="TikTok Script Generator",
38
  description="Generates short Indonesian scripts (JSON)."
39
  )