Skip to content

Instantly share code, notes, and snippets.

@BoeJaker
Created May 13, 2023 14:17
Show Gist options
  • Select an option

  • Save BoeJaker/3ec67eaef359a6bdd73dc60de5b8be32 to your computer and use it in GitHub Desktop.

Select an option

Save BoeJaker/3ec67eaef359a6bdd73dc60de5b8be32 to your computer and use it in GitHub Desktop.
Hugging Face Workbench
import gradio as gr
from transformers import pipeline
import time
import requests
model = "gpt2"
generator = pipeline("text-generation", model=model)
generator.model.config.temperature = 0.6
generator.model.config.repetition_penalty = 1.5
generator.model.config.length_penalty = 2
generator.model.config.max_length = 500
model_info = generator.model.config
def add_text(history, text):
history = history + [(text, None)]
return history, ""
def add_file(history, file):
history = history + [((file.name,), None)]
return history
def bot(history):
# Get the last user input from the history
user_input = history[-1][0]
# Generate a response using the Hugging Face model
response = generator(user_input, do_sample=True)[0]["generated_text"]
# history[-1][1] = response
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.05)
yield history
with gr.Blocks() as demo:
gr.Markdown("""# """ + model.upper())
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
with gr.Row():
with gr.Column(scale=0.85):
txt = gr.Textbox(
show_label=False,
placeholder="Enter text and press enter, or upload an image",
).style(container=False)
with gr.Column(scale=0.15, min_width=0):
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
bot, chatbot, chatbot
)
btn.upload(add_file, [chatbot, btn], [chatbot]).then(
bot, chatbot, chatbot
)
gr.Markdown(
"""## Parameters\n<br><table><tr><th>Name</th><th>Value</th></tr>""" +
"".join(f"<tr><td><b>{key}</b></td><td>{value}</td></tr>" for key, value in model_info.__dict__.items()) +
"""</table>"""
)
demo.queue()
demo.launch(share=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment