Calvin commited on
Commit
9b51f9d
Β·
1 Parent(s): 8b89546

fix model

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -6,6 +6,7 @@ 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
 
@@ -18,25 +19,30 @@ def clean_and_extract_json(text):
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,
@@ -44,23 +50,13 @@ def generate_ui(prompt):
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) #add api
 
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
 
 
19
  text = re.sub(r"```json|```", "", text).strip()
20
  return text
21
 
 
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
+
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 in JSON")
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!"
47
  return {
48
  "script": fallback_script,
 
50
  "hashtags": ["#indonesia", "#fyp"]
51
  }
52
 
53
+ demo = gr.Interface(
54
+ fn=generate_ui,
55
+ inputs=gr.Textbox(lines=6, placeholder="Masukkan prompt..."),
56
+ outputs=gr.JSON(),
57
+ title="TikTok Script Generator",
58
+ description="Generates short Indonesian scripts (JSON)."
59
  )
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
+ demo.launch(server_name="0.0.0.0", port=7860, enable_api=True)