Spaces:
Running
Running
Add zero-GPU Gradio demo
Browse files- .hf/hf_space_config.json +4 -0
- app.py +55 -0
- requirements.txt +3 -0
.hf/hf_space_config.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"size": "cpu-basic",
|
3 |
+
"python_file": "app.py"
|
4 |
+
}
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load your fine-tuned model from the Hub
|
5 |
+
MODEL_ID = "tasal9/ZamAI-mT5-Pashto"
|
6 |
+
generator = pipeline(
|
7 |
+
"text2text-generation",
|
8 |
+
model=MODEL_ID,
|
9 |
+
tokenizer=MODEL_ID,
|
10 |
+
device=-1 # CPU only
|
11 |
+
)
|
12 |
+
|
13 |
+
# Prompt template
|
14 |
+
|
15 |
+
def generate_prompt(instruction, input_text=""):
|
16 |
+
if input_text:
|
17 |
+
return (
|
18 |
+
f"Below is an instruction that describes a task, paired with an input that provides further context. "
|
19 |
+
f"Write a response that appropriately completes the request.\n\n"
|
20 |
+
f"### Instruction:\n{instruction}\n\n"
|
21 |
+
f"### Input:\n{input_text}\n\n"
|
22 |
+
f"### Response:"
|
23 |
+
)
|
24 |
+
else:
|
25 |
+
return (
|
26 |
+
f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n"
|
27 |
+
f"### Instruction:\n{instruction}\n\n"
|
28 |
+
f"### Response:"
|
29 |
+
)
|
30 |
+
|
31 |
+
# Inference function
|
32 |
+
def predict(instruction, input_text):
|
33 |
+
prompt = generate_prompt(instruction, input_text)
|
34 |
+
outputs = generator(
|
35 |
+
prompt,
|
36 |
+
max_length=256,
|
37 |
+
num_beams=5,
|
38 |
+
early_stopping=True
|
39 |
+
)
|
40 |
+
return outputs[0]["generated_text"]
|
41 |
+
|
42 |
+
# Gradio interface
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=predict,
|
45 |
+
inputs=[
|
46 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter instruction here...", label="Instruction"),
|
47 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter optional input here...", label="Input")
|
48 |
+
],
|
49 |
+
outputs=gr.outputs.Textbox(label="Response"),
|
50 |
+
title="ZamAI mT5 Pashto Demo",
|
51 |
+
description="A zero-GPU Gradio demo for the ZamAI-mT5-Pashto model."
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers>=4.42.4
|
3 |
+
torch
|