import os
os.system('pip install -q git+https://github.com/huggingface/transformers.git')
os.system('pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu')
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
import torch
import gradio as gr
import re
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class GUI:
def query(self,query,modelo="flan-t5-small",tokens=100):
options=""
tok_len=tokens
t5query = f"""Question: "{query}" Context: {options}"""
if (modelo=="flan-t5-small" or modelo=="flan-t5-large"):
tokenizer = AutoTokenizer.from_pretrained("google/{}".format(modelo))
model = AutoModelForSeq2SeqLM.from_pretrained("google/{}".format(modelo)).to(device)
inputs = tokenizer(t5query, return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=tok_len)
else:
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M").to(device)
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
input_ids = tokenizer(t5query, return_tensors="pt").to(device)
outputs = model.generate(**input_ids, do_sample=True, max_length=tok_len)
generation=tokenizer.batch_decode(outputs, skip_special_tokens=True)
return '\n'.join(generation)
def begin(self,question,modelo,tokens):
results = app.query(question,tokens)
return results
app = GUI()
title = "Get answers with questions with Flan-T5"
description = "Results will show up in a few seconds."
article="More info ruslanmv.com
"
css = """.output_image, .input_image {height: 600px !important}"""
iface = gr.Interface(fn=app.begin,
inputs=[ gr.Textbox(label="Question"),
gr.Radio(["flan-t5-small", "flan-t5-large","gpt-neo-125M"],label="Model",value="flan-t5-small"),
gr.Slider(30, 200, value=100, step = 1,label="Max Tokens"),],
outputs = gr.Text(label="Answer Summary"),
title=title,
description=description,
article=article,
css=css,
analytics_enabled = True
,enable_queue=True)
iface.launch(inline=False, share=False, debug=False)