Spaces:
Runtime error
Runtime error
Commit ·
f343046
1
Parent(s): 2440d55
Add application file
Browse files- app.py +61 -3
- requirements.txt +3 -0
app.py
CHANGED
|
@@ -1,7 +1,65 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
+
# ===== Model setup =====
|
| 6 |
+
model_path = "protonx-models/protonx-legal-tc"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 10 |
+
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model.to(device)
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
MAX_TOKENS = 160
|
| 16 |
+
|
| 17 |
+
# ===== Inference function =====
|
| 18 |
+
def generate(text):
|
| 19 |
+
if not text or not text.strip():
|
| 20 |
+
return ""
|
| 21 |
+
|
| 22 |
+
inputs = tokenizer(
|
| 23 |
+
text,
|
| 24 |
+
return_tensors="pt",
|
| 25 |
+
truncation=True,
|
| 26 |
+
max_length=MAX_TOKENS
|
| 27 |
+
).to(device)
|
| 28 |
+
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model.generate(
|
| 31 |
+
**inputs,
|
| 32 |
+
num_beams=10,
|
| 33 |
+
num_return_sequences=1,
|
| 34 |
+
max_new_tokens=MAX_TOKENS,
|
| 35 |
+
early_stopping=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
+
return decoded
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# ===== Gradio UI =====
|
| 43 |
+
with gr.Blocks(title="ProtonX Legal Text Generation") as demo:
|
| 44 |
+
gr.Markdown("## 🧾 ProtonX Legal Text Generation")
|
| 45 |
+
|
| 46 |
+
input_text = gr.Textbox(
|
| 47 |
+
label="Input text",
|
| 48 |
+
placeholder="Nhập nội dung pháp lý...",
|
| 49 |
+
lines=6
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
submit_btn = gr.Button("Submit")
|
| 53 |
+
|
| 54 |
+
output_text = gr.Textbox(
|
| 55 |
+
label="Output",
|
| 56 |
+
lines=6
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
submit_btn.click(
|
| 60 |
+
fn=generate,
|
| 61 |
+
inputs=input_text,
|
| 62 |
+
outputs=output_text
|
| 63 |
+
)
|
| 64 |
|
|
|
|
| 65 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|