File size: 2,663 Bytes
98739be
 
28a3cc7
98739be
28a3cc7
98739be
28a3cc7
 
98739be
28a3cc7
 
98739be
 
28a3cc7
 
 
 
 
98739be
28a3cc7
98739be
 
 
28a3cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98739be
28a3cc7
 
 
 
 
 
 
 
98739be
28a3cc7
 
 
 
 
 
 
98739be
28a3cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98739be
28a3cc7
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
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("""
    <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")
    
    # 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)