Spaces:
Running
Running
First commit
Browse files
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 💻
|
4 |
colorFrom: blue
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: FLAN-T5 vs GTP
|
3 |
emoji: 💻
|
4 |
colorFrom: blue
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.20.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system('pip install -q git+https://github.com/huggingface/transformers.git')
|
3 |
+
os.system('pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu')
|
4 |
+
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
6 |
+
import torch
|
7 |
+
import gradio as gr
|
8 |
+
import re
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
class GUI:
|
11 |
+
def query(self,query,modelo="flan-t5-small",tokens=100):
|
12 |
+
options=""
|
13 |
+
tok_len=tokens
|
14 |
+
t5query = f"""Question: "{query}" Context: {options}"""
|
15 |
+
if (modelo=="flan-t5-small" or modelo=="flan-t5-large"):
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("google/{}".format(modelo))
|
17 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/{}".format(modelo)).to(device)
|
18 |
+
inputs = tokenizer(t5query, return_tensors="pt").to(device)
|
19 |
+
outputs = model.generate(**inputs, max_new_tokens=tok_len)
|
20 |
+
else:
|
21 |
+
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M").to(device)
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
|
23 |
+
input_ids = tokenizer(t5query, return_tensors="pt").to(device)
|
24 |
+
outputs = model.generate(**input_ids, do_sample=True, max_length=tok_len)
|
25 |
+
generation=tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
26 |
+
|
27 |
+
return '\n'.join(generation)
|
28 |
+
def begin(self,question,modelo,tokens):
|
29 |
+
results = app.query(question,tokens)
|
30 |
+
return results
|
31 |
+
|
32 |
+
app = GUI()
|
33 |
+
title = "Get answers with questions with Flan-T5"
|
34 |
+
description = "Results will show up in a few seconds."
|
35 |
+
article="More info <a href='https://ruslanmv.com/'>ruslanmv.com</a><br>"
|
36 |
+
css = """.output_image, .input_image {height: 600px !important}"""
|
37 |
+
|
38 |
+
iface = gr.Interface(fn=app.begin,
|
39 |
+
inputs=[ gr.Textbox(label="Question"),
|
40 |
+
gr.Radio(["flan-t5-small", "flan-t5-large","gpt-neo-125M"],label="Model",value="flan-t5-small"),
|
41 |
+
gr.Slider(30, 200, value=100, step = 1,label="Max Tokens"),],
|
42 |
+
outputs = gr.Text(label="Answer Summary"),
|
43 |
+
title=title,
|
44 |
+
description=description,
|
45 |
+
article=article,
|
46 |
+
css=css,
|
47 |
+
analytics_enabled = True
|
48 |
+
,enable_queue=True)
|
49 |
+
iface.launch(inline=False, share=False, debug=False)
|