|
|
|
from src.utils import (get_output, show_output) |
|
from src.constants import TASKS |
|
import streamlit as st |
|
import json |
|
|
|
with st.sidebar: |
|
st.markdown( |
|
"## How to use\n" |
|
"1. Enter your [Huggingface API token](https://huggingface.co/settings/tokens) (Recommended, as some model won't work)\n" |
|
"2. Choose a task to perform \n" |
|
"3. Choose a LLM model correspong to task \n" |
|
"4. Enter a different LLM model id from Huggingface hub (Optional) \n" |
|
) |
|
|
|
st.write("##") |
|
|
|
|
|
huggingface_api_token = st.text_input("Huggingface API Token (Optional)", |
|
key="hf_api_token", |
|
type="password", |
|
help="You can get your API token from https://huggingface.co/settings/tokens." |
|
) |
|
|
|
|
|
task = st.selectbox("Choose a task", list(TASKS.keys())) |
|
|
|
|
|
select_model = st.selectbox("Choose a model", TASKS[task]) |
|
|
|
|
|
optional_model = st.text_input("Enter Huggingface model (Optional)") |
|
|
|
if (optional_model != ""): |
|
model = optional_model |
|
else: |
|
model = select_model |
|
|
|
|
|
st.header("Huggingface Hub Model Explorer") |
|
|
|
|
|
st.subheader(f"{task} with {model}") |
|
|
|
|
|
if task == "Text Generation": |
|
|
|
text_input = st.text_input("Enter some text to generate from") |
|
|
|
|
|
generate_button = st.button("Generate") |
|
|
|
|
|
if generate_button and text_input: |
|
|
|
data = json.dumps({"inputs": text_input}) |
|
|
|
|
|
output = get_output(task, model, data, api_token=huggingface_api_token) |
|
|
|
|
|
show_output(output) |
|
|
|
elif task == "Text Summarization": |
|
|
|
text_area = st.text_area("Enter some text to summarize") |
|
|
|
|
|
summarize_button = st.button("Summarize") |
|
|
|
|
|
if summarize_button and text_area: |
|
|
|
data = json.dumps({"inputs": text_area}) |
|
|
|
|
|
output = get_output(task, model, data, api_token=huggingface_api_token) |
|
|
|
|
|
show_output(output) |
|
|
|
elif task == "Text Classification": |
|
|
|
text_input = st.text_input("Enter some text to classify") |
|
|
|
|
|
classify_button = st.button("Classify") |
|
|
|
|
|
if classify_button and text_input: |
|
|
|
data = json.dumps({"inputs": text_input}) |
|
|
|
|
|
output = get_output(task, model, data, api_token=huggingface_api_token) |
|
|
|
|
|
ref_output = output[0] |
|
for out in ref_output: |
|
st.text(f"{out['label']}: {round(out['score'] * 100, 1)}%") |
|
|
|
elif task == "Text-to-Speech": |
|
|
|
text_input = st.text_input("Enter some text to synthesize") |
|
|
|
|
|
synthesize_button = st.button("Synthesize") |
|
|
|
|
|
if synthesize_button and text_input: |
|
|
|
data = json.dumps({"inputs": text_input}) |
|
|
|
|
|
output = get_output(task, model, data, api_token=huggingface_api_token, model_type="audio") |
|
|
|
|
|
with open("audio.wav", "wb") as f: |
|
f.write(output.content) |
|
st.audio("audio.wav") |
|
|
|
elif task == "Image Classification": |
|
|
|
image_file = st.file_uploader("Upload an image to classify") |
|
|
|
|
|
classify_button = st.button("Classify") |
|
|
|
|
|
output_widget = st.empty() |
|
|
|
|
|
if classify_button and image_file: |
|
|
|
image_bytes = image_file.read() |
|
|
|
|
|
output = get_output(task, model, data=image_bytes, api_token=huggingface_api_token) |
|
|
|
|
|
for items in output: |
|
st.text(f"{items['label']}: {round(items['score'] * 100, 1)}%") |