File size: 3,859 Bytes
f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 49885d4 f80dc67 16c3f1f f80dc67 16c3f1f f80dc67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import gradio as gr
from gradio import ChatInterface, Request
import anyio
import os
import threading
import sys
from itertools import chain
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
LOG_LEVEL = "INFO"
TIMEOUT = 60
# Load Hugging Face model and tokenizer
model_name = "gpt2" # You can change this to any other model available on Hugging Face
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Define function to generate responses using the Hugging Face model
def generate_response(message, history):
inputs = tokenizer(message, return_tensors="pt")
outputs = model.generate(**inputs, max_length=150, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
class myChatInterface(ChatInterface):
async def _submit_fn(
self,
message: str,
history_with_input: list[list[str | None]],
request: Request,
*args,
) -> tuple[list[list[str | None]], list[list[str | None]]]:
history = history_with_input[:-1]
response = generate_response(message, history)
history.append([message, response])
return history, history
with gr.Blocks() as demo:
def flatten_chain(list_of_lists):
return list(chain.from_iterable(list_of_lists))
class thread_with_trace(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
self._return = None
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)
def globaltrace(self, frame, event, arg):
if event == "call":
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == "line":
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True
def join(self, timeout=0):
threading.Thread.join(self, timeout)
return self._return
def get_description_text():
return """
# Hugging Face Model Chatbot Demo
This demo shows how to build a chatbot using models available on Hugging Face.
"""
description = gr.Markdown(get_description_text())
with gr.Row() as params:
txt_model = gr.Dropdown(
label="Model",
choices=[
"gpt2",
"gpt-2-medium",
"gpt-2-large",
"gpt-2-xl",
],
allow_custom_value=True,
value="gpt2",
container=True,
)
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
avatar_images=(
"human.png",
(os.path.join(os.path.dirname(__file__), "autogen.png")),
),
render=False,
height=600,
)
txt_input = gr.Textbox(
scale=4,
show_label=False,
placeholder="Enter text and press enter",
container=False,
render=False,
autofocus=True,
)
chatiface = myChatInterface(
respond=None,
chatbot=chatbot,
textbox=txt_input,
additional_inputs=[txt_model],
)
if __name__ == "__main__":
demo.launch(share=True, server_name="0.0.0.0")
|