Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,95 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
gr.Interface.load("models/s3nh/pythia-410m-70k-steps-self-instruct-polish").launch()
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
import gradio as gr
|
| 3 |
+
import transformers
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
from transformers import ModelForCausalLM
|
| 6 |
+
from transformers import GenerationConfig
|
| 7 |
+
from typing import List, Dict, Union
|
| 8 |
+
from typing import Any, TypeVar
|
| 9 |
+
|
| 10 |
+
Pathable = Union[str, pathlib.Path]
|
| 11 |
+
|
| 12 |
+
def load_model(name: str) -> Any:
|
| 13 |
+
return ModelForCausalLM.from_pretrained(name)
|
| 14 |
+
|
| 15 |
+
def load_tokenizer(name: str) -> Any:
|
| 16 |
+
return AutoTokenizer.from_pretrained(name)
|
| 17 |
+
|
| 18 |
+
def create_generator():
|
| 19 |
+
return GenerationConfig(
|
| 20 |
+
temperature=1.0,
|
| 21 |
+
top_p=0.75,
|
| 22 |
+
num_beams=4,
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
def generate_prompt(instruction, input=None):
|
| 26 |
+
if input:
|
| 27 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 28 |
+
|
| 29 |
+
### Instruction:
|
| 30 |
+
{instruction}
|
| 31 |
+
|
| 32 |
+
### Input:
|
| 33 |
+
{input}
|
| 34 |
+
|
| 35 |
+
### Response:"""
|
| 36 |
+
else:
|
| 37 |
+
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 38 |
+
|
| 39 |
+
### Instruction:
|
| 40 |
+
{instruction}
|
| 41 |
+
|
| 42 |
+
### Response:"""
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def evaluate(instruction, input=None):
|
| 48 |
+
prompt = generate_prompt(instruction, input)
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 50 |
+
input_ids = inputs["input_ids"].cuda()
|
| 51 |
+
generation_output = model.generate(
|
| 52 |
+
input_ids=input_ids,
|
| 53 |
+
generation_config=generation_config,
|
| 54 |
+
return_dict_in_generate=True,
|
| 55 |
+
output_scores=True,
|
| 56 |
+
max_new_tokens=256
|
| 57 |
+
)
|
| 58 |
+
for s in generation_output.sequences:
|
| 59 |
+
output = tokenizer.decode(s)
|
| 60 |
+
print("Response:", output.split("### Response:")[1].strip())
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def inference(text):
|
| 64 |
+
output = evaluate(instruction = instruction, input = input)
|
| 65 |
+
return output
|
| 66 |
+
|
| 67 |
+
io = gr.Interface(
|
| 68 |
+
inference,
|
| 69 |
+
gr.Textbox(
|
| 70 |
+
lines = 3, max_lines = 10,
|
| 71 |
+
placeholder = "Add question here",
|
| 72 |
+
interactive = True,
|
| 73 |
+
show_label = False
|
| 74 |
+
),
|
| 75 |
+
gr.Textbox(
|
| 76 |
+
lines = 3,
|
| 77 |
+
max_lines = 25,
|
| 78 |
+
placeholder = "add context here",
|
| 79 |
+
interactive = True,
|
| 80 |
+
show_label = False
|
| 81 |
+
),
|
| 82 |
+
outputs =[
|
| 83 |
+
gr.Textbox(lines = 2, label = 'Pythia410m output', interactive = False)
|
| 84 |
+
]
|
| 85 |
+
),
|
| 86 |
+
title = title,
|
| 87 |
+
description = description,
|
| 88 |
+
article = article,
|
| 89 |
+
examples = examples,
|
| 90 |
+
cache_examples = False,
|
| 91 |
+
)
|
| 92 |
+
io.launch()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
#gr.Interface.load("models/s3nh/pythia-410m-70k-steps-self-instruct-polish").launch()
|