Spaces:
Sleeping
Sleeping
File size: 2,581 Bytes
9a9e471 3fba009 191985e 9a9e471 191985e 9a9e471 191985e 9a9e471 14e02dc 3fba009 9a9e471 191985e 9a9e471 191985e 9a9e471 191985e 3fba009 14e02dc 3fba009 191985e 9a9e471 191985e 9a9e471 191985e 9a9e471 191985e 9a9e471 191985e 9a9e471 191985e 9a9e471 191985e |
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 |
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
from transformers import logging
# Hugging Face Transformers kütüphanesinden gelen hataları kapat
logging.set_verbosity_error()
# Streamlit başlık
st.title('Chat with Me')
# Model yükleme (burada dil modeli veya sohbet modeli olmalı)
model = load_model('text_classification_model.h5')
# Modelin beklediği giriş boyutunu öğrenme
model_input_shape = model.layers[0].input_shape[1]
# Tokenizer yükleme veya tanımlama
tokenizer = Tokenizer(num_words=10000) # num_words, kelime sayısı limitidir, modelinize göre ayarlayabilirsiniz
tokenizer.fit_on_texts(['örnek metin']) # Tokenizer'ı eğitmek için örnek bir metin kullanabilirsiniz
# Chat history
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
# Kullanıcıdan giriş alma
soru = st.text_input('Sor:')
# Yanıt üretme fonksiyonu (modelinize göre bu kısım değişebilir)
def generate_response(input_text):
# Metni tokenize et ve sekanslara dönüştür
sequences = tokenizer.texts_to_sequences([input_text])
padded_sequences = pad_sequences(sequences, maxlen=model_input_shape) # maxlen, modelin beklediği giriş boyutuna göre ayarlanmalıdır
prediction = model.predict(padded_sequences)
response = "Bu bir örnek yanıttır." # Modelden gelen yanıtı buraya yazın
return response
# "Sor" butonuna tıklama olayını işleme
if st.button('Sor'):
if soru:
response = generate_response(soru)
st.session_state.chat_history.append({'role': 'user', 'text': soru})
st.session_state.chat_history.append({'role': 'model', 'text': response})
st.experimental_rerun()
# Sohbet geçmişini gösterme
for message in reversed(st.session_state.chat_history):
if message['role'] == 'user':
st.markdown(f'<div style="text-align: right; background-color: #2F2F2F; padding: 10px; border-radius: 10px; margin: 10px; width: fit-content;">👤 Sen: {message["text"]}</div>', unsafe_allow_html=True)
elif message['role'] == 'model':
st.markdown(f'<div style="text-align: left; background-color: #2E2E2E; padding: 10px; border-radius: 10px; margin: 10px; width: fit-content;">🤖 Bot: {message["text"]}</div>', unsafe_allow_html=True)
# "Yeni Sohbet" butonuna tıklama olayını işleme
if st.button('Yeni Sohbet'):
st.session_state.chat_history = []
st.experimental_rerun()
|