File size: 2,808 Bytes
540b86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b046bfe
540b86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19b576a
540b86f
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
import gradio as gr
from huggingface_hub import InferenceClient

client = InferenceClient(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")

def respond(message, history, system_message):
    # Mesajları hazırlıyoruz, sistem mesajı ile başlıyoruz
    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})

    # Son kullanıcı mesajını ekliyoruz
    messages.append({"role": "user", "content": message})

    response = ""
    try:
        # Yanıtları alıyoruz ve yayınlıyoruz
        result = client.chat_completion(
            messages=messages,
            max_tokens=250,
            temperature=0.7,
            top_p=0.95,
        )
        for choice in result.choices:
            response += choice.message.get('content', '')
        yield response
    except Exception as e:
        # Hata durumunda hata mesajını döndürüyoruz
        yield f"Hata: {e}"

# Gradio arayüzünü oluşturuyoruz
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")
    
    # Butonları yan yana koymak için bir satır içine alıyoruz
    with gr.Row():
        clear = gr.Button("Temizle")
        submit = gr.Button("Gönder")

    def user_input(user_message, history):
        # Mesajı HTML ile resim ekleyerek döndürme
        user_message_with_image = f'<img src="file_path/favicon.ico" alt="icon" style="width: 16px; height: 16px; vertical-align: middle;"> {user_message}'
        return "", history + [[user_message_with_image, 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)  # Gönder butonuna tıklandığında mesajı gönder

# Uygulamayı başlatıyoruz
if __name__ == "__main__":
    demo.launch(share=True)