test6 / app.py
anezatra2's picture
Update app.py
540b86f verified
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)