import gradio as gr from huggingface_hub import InferenceClient from transformers import AutoTokenizer client = InferenceClient(model="AriakimTaiyo/gpt2-chat") # Load the tokenizer explicitly tokenizer = AutoTokenizer.from_pretrained("AriakimTaiyo/gpt2-chat") def respond(message, history, system_message): # Prepare messages, starting with the 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}) # Add the latest user message messages.append({"role": "user", "content": message}) response = "" try: # Generate responses 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: # Return the error message in case of an exception yield f"Hata: {e}" # Create the Gradio interface with gr.Blocks(theme=gr.Theme.from_hub('HaleyCH/HaleyCH_Theme')) as demo: system_message = gr.HTML("""

SIMULACRA GPT-2

🤖 Welcome to Simulacra user! See our account for more information.

""") chatbot = gr.Chatbot() msg = gr.Textbox(label="Mesajınızı yazın") # Place buttons side by side 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) # Send the message when the "Gönder" button is clicked # Launch the app if __name__ == "__main__": demo.launch(share=True)