Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,6 @@ import re
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
from transformers import (
|
| 6 |
-
AutoTokenizer,
|
| 7 |
-
AutoModelForSeq2SeqLM
|
| 8 |
-
)
|
| 9 |
-
|
| 10 |
-
import re
|
| 11 |
-
import gradio as gr
|
| 12 |
-
import torch
|
| 13 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 14 |
|
| 15 |
MODEL_NAME = "google/flan-t5-small"
|
|
@@ -17,11 +9,14 @@ MAX_INPUT_TOKENS = 512
|
|
| 17 |
MAX_OUTPUT_TOKENS = 300
|
| 18 |
|
| 19 |
print("Loading model...")
|
|
|
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 22 |
model.eval()
|
|
|
|
| 23 |
print("Model loaded.")
|
| 24 |
|
|
|
|
| 25 |
# -----------------------------
|
| 26 |
# Utilities
|
| 27 |
# -----------------------------
|
|
@@ -68,7 +63,7 @@ def build_prompt(mode: str, text: str) -> str:
|
|
| 68 |
|
| 69 |
|
| 70 |
# -----------------------------
|
| 71 |
-
#
|
| 72 |
# -----------------------------
|
| 73 |
|
| 74 |
@torch.inference_mode()
|
|
@@ -92,7 +87,7 @@ def generate_text(prompt: str) -> str:
|
|
| 92 |
|
| 93 |
|
| 94 |
# -----------------------------
|
| 95 |
-
#
|
| 96 |
# -----------------------------
|
| 97 |
|
| 98 |
def process(text: str, mode: str) -> str:
|
|
@@ -105,7 +100,7 @@ def process(text: str, mode: str) -> str:
|
|
| 105 |
|
| 106 |
|
| 107 |
# -----------------------------
|
| 108 |
-
# Gradio
|
| 109 |
# -----------------------------
|
| 110 |
|
| 111 |
demo = gr.Interface(
|
|
@@ -120,14 +115,9 @@ demo = gr.Interface(
|
|
| 120 |
],
|
| 121 |
outputs=gr.Textbox(lines=8, label="Output"),
|
| 122 |
title="Free Text Rewrite & Generation Service",
|
| 123 |
-
description=
|
| 124 |
-
"Fast, lightweight AI service using flan-t5-small.\n"
|
| 125 |
-
"Supports rewriting, normalization, short definitions, and article generation."
|
| 126 |
-
),
|
| 127 |
-
allow_flagging="never"
|
| 128 |
)
|
| 129 |
|
| 130 |
-
# IMPORTANT: no queue (avoids async / stuck Space issues)
|
| 131 |
demo.queue(False)
|
| 132 |
|
| 133 |
demo.launch(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 6 |
|
| 7 |
MODEL_NAME = "google/flan-t5-small"
|
|
|
|
| 9 |
MAX_OUTPUT_TOKENS = 300
|
| 10 |
|
| 11 |
print("Loading model...")
|
| 12 |
+
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 15 |
model.eval()
|
| 16 |
+
|
| 17 |
print("Model loaded.")
|
| 18 |
|
| 19 |
+
|
| 20 |
# -----------------------------
|
| 21 |
# Utilities
|
| 22 |
# -----------------------------
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
# -----------------------------
|
| 66 |
+
# Inference
|
| 67 |
# -----------------------------
|
| 68 |
|
| 69 |
@torch.inference_mode()
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
# -----------------------------
|
| 90 |
+
# API function
|
| 91 |
# -----------------------------
|
| 92 |
|
| 93 |
def process(text: str, mode: str) -> str:
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
# -----------------------------
|
| 103 |
+
# Gradio app
|
| 104 |
# -----------------------------
|
| 105 |
|
| 106 |
demo = gr.Interface(
|
|
|
|
| 115 |
],
|
| 116 |
outputs=gr.Textbox(lines=8, label="Output"),
|
| 117 |
title="Free Text Rewrite & Generation Service",
|
| 118 |
+
description="Lightweight rewrite, normalization, definitions, and articles using flan-t5-small."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
|
|
|
|
| 121 |
demo.queue(False)
|
| 122 |
|
| 123 |
demo.launch(
|