|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
from transformers import AutoTokenizer |
|
|
|
client = InferenceClient(model="AriakimTaiyo/gpt2-chat") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("AriakimTaiyo/gpt2-chat") |
|
|
|
def respond(message, history, system_message): |
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
for user_msg, assistant_msg in history: |
|
if user_msg: |
|
messages.append({"role": "user", "content": user_msg}) |
|
if assistant_msg: |
|
messages.append({"role": "assistant", "content": assistant_msg}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
response = "" |
|
try: |
|
|
|
for message in client.chat_completion( |
|
messages=messages, |
|
max_tokens=256, |
|
stream=True, |
|
temperature=0.7, |
|
top_p=0.95, |
|
): |
|
token = message.choices[0].delta.get('content', '') |
|
response += token |
|
yield response |
|
except Exception as e: |
|
|
|
yield f"Hata: {e}" |
|
|
|
|
|
with gr.Blocks(theme=gr.Theme.from_hub('HaleyCH/HaleyCH_Theme')) as demo: |
|
system_message = gr.HTML(""" |
|
<h1 style="color: #fff; text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 10px #0000ff, 0 0 15px #0000ff; text-align: center;"> |
|
SIMULACRA GPT-2 |
|
</h1> |
|
<p>🤖 Welcome to Simulacra user! See our account for more information.</p> |
|
""") |
|
|
|
chatbot = gr.Chatbot() |
|
msg = gr.Textbox(label="Mesajınızı yazın") |
|
|
|
|
|
with gr.Row(): |
|
clear = gr.Button("Temizle") |
|
submit = gr.Button("Gönder") |
|
|
|
def user_input(user_message, history): |
|
return "", history + [[user_message, None]] |
|
|
|
def bot_response(history): |
|
last_message = history[-1][0] |
|
response_gen = respond( |
|
message=last_message, |
|
history=history[:-1], |
|
system_message=system_message.value, |
|
) |
|
for response in response_gen: |
|
history[-1][1] = response |
|
yield history |
|
|
|
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then( |
|
bot_response, chatbot, chatbot |
|
) |
|
clear.click(lambda: None, None, chatbot, queue=False) |
|
submit.click(lambda: msg.submit(), None, chatbot, queue=False) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|