Spaces:
Runtime error
Runtime error
Calvin commited on
Commit Β·
18a35e1
1
Parent(s): d724f60
skeleton
Browse files- app.py +35 -0
- requirement.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 12 |
+
generator = pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=model,
|
| 15 |
+
tokenizer=tokenizer,
|
| 16 |
+
device=-1, # CPU
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def generate(prompt):
|
| 20 |
+
# prompt already instructs JSON-only output; but we keep generation stable
|
| 21 |
+
out = generator(prompt, max_new_tokens=180, do_sample=True, top_p=0.95, top_k=40, temperature=0.8)
|
| 22 |
+
text = out[0]["generated_text"]
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
# Simple gradio UI (the same function is exposed as API)
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=generate,
|
| 28 |
+
inputs=gr.Textbox(lines=6, placeholder="Prompt..."),
|
| 29 |
+
outputs=gr.Textbox(),
|
| 30 |
+
title="TikTok Script Generator",
|
| 31 |
+
description="Generates short Indonesian scripts (JSON)."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirement.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
torch # use default - HF will pull a CPU build
|