Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import httpx
|
4 |
+
|
5 |
+
def geninference(model):
|
6 |
+
modelconfig = httpx.get(f'https://huggingface.co/{model}/raw/main/config.json')
|
7 |
+
jsonconfig = json.loads(modelconfig.text)
|
8 |
+
rawtokenizerconfig = httpx.get(f'https://huggingface.co/{model}/raw/main/tokenizer_config.json')
|
9 |
+
tokenizerconfig = json.loads(rawtokenizerconfig.text)
|
10 |
+
arch = jsonconfig["architectures"][0]
|
11 |
+
tokenizerclass = tokenizerconfig["tokenizer_class"]
|
12 |
+
inference_code = f"""
|
13 |
+
from transformers import {arch}, {tokenizerclass}
|
14 |
+
|
15 |
+
model_id = "{model}"
|
16 |
+
|
17 |
+
model = {arch}.from_pretrained(model_id)
|
18 |
+
tokenizer = {tokenizerclass}.from_pretrained(model_id)
|
19 |
+
prompt = "Hello"
|
20 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
21 |
+
gen_ids = model.generate(
|
22 |
+
inputs.input_ids,
|
23 |
+
max_length=50,
|
24 |
+
num_return_sequences=1,
|
25 |
+
no_repeat_ngram_size=2,
|
26 |
+
do_sample=True,
|
27 |
+
temperature=0.7,
|
28 |
+
)
|
29 |
+
output = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
30 |
+
print(output)
|
31 |
+
"""
|
32 |
+
return inference_code
|
33 |
+
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("## Generate inference code for your model with a single click!")
|
36 |
+
with gr.Row(equal_height=True):
|
37 |
+
inputbox = gr.Textbox(placeholder="Enter your model id here", label="Model ID")
|
38 |
+
generatebtn = gr.Button("Generate", variant="primary")
|
39 |
+
outputbox = gr.Textbox(placeholder="Output will be here", label="output", interactive=False)
|
40 |
+
generatebtn.click(
|
41 |
+
fn=geninference,
|
42 |
+
inputs=inputbox,
|
43 |
+
outputs=outputbox
|
44 |
+
)
|
45 |
+
|
46 |
+
demo.launch()
|