Spaces:
Sleeping
Sleeping
test
Browse files- app.py +61 -60
- requirements.txt +3 -0
app.py
CHANGED
|
@@ -1,70 +1,71 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# ---- Config ----
|
| 8 |
+
MODEL_ID = os.getenv("HF_MODEL_ID", "GroNLP/gpt2-small-dutch")
|
| 9 |
+
# Set CPU threads modestly to avoid aggressive memory/CPU usage in Spaces
|
| 10 |
+
torch.set_num_threads(2)
|
| 11 |
|
| 12 |
+
# ---- Model load ----
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Create a pipeline suitable for CPU (device=-1)
|
| 17 |
+
generator = pipeline(
|
| 18 |
+
"text-generation",
|
| 19 |
+
model=model,
|
| 20 |
+
tokenizer=tokenizer,
|
| 21 |
+
device=-1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# ---- Simple prompt-based simplifier ----
|
| 25 |
+
def simplify(text: str, max_new_tokens: int = 60) -> str:
|
| 26 |
+
if not text or not text.strip():
|
| 27 |
+
return "Voer eerst wat tekst in."
|
| 28 |
+
# Prompt: instruct the model to produce a simplified version.
|
| 29 |
+
prompt = (
|
| 30 |
+
"Vereenvoudig de volgende tekst naar helder, begrijpelijk Nederlands.\n\n"
|
| 31 |
+
"Brontekst:\n"
|
| 32 |
+
f"{text}\n\n"
|
| 33 |
+
"Vereenvoudigde versie:"
|
| 34 |
+
)
|
| 35 |
+
# deterministic generation (no sampling) to keep output stable on CPU
|
| 36 |
+
out = generator(
|
| 37 |
+
prompt,
|
| 38 |
+
max_new_tokens=int(max_new_tokens),
|
| 39 |
+
do_sample=False,
|
| 40 |
+
num_return_sequences=1,
|
| 41 |
+
)[0]["generated_text"]
|
| 42 |
+
# strip prompt portion; keep everything after the marker
|
| 43 |
+
if "Vereenvoudigde versie:" in out:
|
| 44 |
+
simplified = out.split("Vereenvoudigde versie:")[-1].strip()
|
| 45 |
+
else:
|
| 46 |
+
# fallback: remove prompt prefix if present
|
| 47 |
+
simplified = out.replace(prompt, "").strip()
|
| 48 |
+
# Keep result reasonably short
|
| 49 |
+
return simplified
|
| 50 |
|
| 51 |
+
# ---- Gradio UI ----
|
| 52 |
+
with gr.Blocks(css=".gradio-container {max-width:1100px}") as demo:
|
| 53 |
+
gr.Markdown("# NL tekst vereenvoudiger (test)\nGebruik een klein Nederlands model: GroNLP/gpt2-small-dutch")
|
| 54 |
+
with gr.Row():
|
| 55 |
+
txt = gr.Textbox(label="Brontekst (Nederlands)", lines=8, placeholder="Plak hier een artikel of paragraaf...")
|
| 56 |
+
out = gr.Textbox(label="Vereenvoudigde tekst", lines=8)
|
| 57 |
+
with gr.Row():
|
| 58 |
+
tokens = gr.Slider(minimum=20, maximum=300, value=80, step=10, label="Max nieuwe tokens (lengte)")
|
| 59 |
+
run_btn = gr.Button("Vereenvoudig")
|
| 60 |
+
run_btn.click(fn=simplify, inputs=[txt, tokens], outputs=out)
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=[
|
| 63 |
+
["De Europese Commissie heeft vandaag een nieuw pakket maatregelen aangekondigd om de energiezekerheid te versterken, waarbij landen gevraagd worden hun strategische reserves aan te vullen."],
|
| 64 |
+
["Het experiment toont aan dat de respons van de sensor significant varieert afhankelijk van temperatuur en luchtvochtigheid."],
|
| 65 |
+
],
|
| 66 |
+
inputs=txt,
|
| 67 |
+
)
|
| 68 |
+
gr.Markdown("**Tips:** Als de Space tijdens build OOM geeft, kies een kleiner model of verlaag `max_new_tokens`.")
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=3.39
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
torch>=2.0.0
|