yunuseduran commited on
Commit
fcc6e49
·
verified ·
1 Parent(s): ca87ee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,61 +1,48 @@
 
 
 
1
  import streamlit as st
2
- from tensorflow.keras.models import load_model
3
- from tensorflow.keras.preprocessing.text import Tokenizer
4
- from tensorflow.keras.preprocessing.sequence import pad_sequences
5
- import numpy as np
6
  from transformers import logging
7
 
8
  # Hugging Face Transformers kütüphanesinden gelen hataları kapat
9
  logging.set_verbosity_error()
10
 
11
- # Streamlit başlık
12
- st.title('Chat with Me')
13
 
14
- # Model yükleme (burada dil modeli veya sohbet modeli olmalı)
15
  model = load_model('text_classification_model.h5')
16
 
17
- # Modelin giriş boyutunu öğrenme
18
- model_input_shape = model.layers[0].input_shape
19
- if isinstance(model_input_shape, list):
20
- model_input_shape = model_input_shape[0]
21
- input_length = model_input_shape[1]
22
-
23
- # Tokenizer yükleme veya tanımlama
24
- tokenizer = Tokenizer(num_words=10000) # num_words, kelime sayısı limitidir, modelinize göre ayarlayabilirsiniz
25
- tokenizer.fit_on_texts(['örnek metin']) # Tokenizer'ı eğitmek için örnek bir metin kullanabilirsiniz
26
-
27
  # Chat history
28
- if 'chat_history' not in st.session_state:
29
- st.session_state.chat_history = []
30
 
31
- # Kullanıcıdan giriş alma
32
  soru = st.text_input('Sor:')
33
 
34
- # Yanıt üretme fonksiyonu (modelinize göre bu kısım değişebilir)
35
- def generate_response(input_text):
36
- # Metni tokenize et ve sekanslara dönüştür
37
- sequences = tokenizer.texts_to_sequences([input_text])
38
- padded_sequences = pad_sequences(sequences, maxlen=input_length) # maxlen, modelin beklediği giriş boyutuna göre ayarlanmalıdır
39
- prediction = model.predict(padded_sequences)
40
- response = "Bu bir örnek yanıttır." # Modelden gelen yanıtı buraya yazın
41
- return response
42
 
43
- # "Sor" butonuna tıklama olayını işleme
44
  if st.button('Sor'):
45
  if soru:
46
- response = generate_response(soru)
47
- st.session_state.chat_history.append({'role': 'user', 'text': soru})
48
- st.session_state.chat_history.append({'role': 'model', 'text': response})
 
 
49
  st.experimental_rerun()
50
 
51
- # Sohbet geçmişini gösterme
52
- for message in reversed(st.session_state.chat_history):
53
- if message['role'] == 'user':
54
  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)
55
- elif message['role'] == 'model':
56
  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)
57
 
58
- # "Yeni Sohbet" butonuna tıklama olayını işleme
59
  if st.button('Yeni Sohbet'):
60
- st.session_state.chat_history = []
61
- st.experimental_rerun()
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
  import streamlit as st
5
+ import google.generativeai as genai
 
 
 
6
  from transformers import logging
7
 
8
  # Hugging Face Transformers kütüphanesinden gelen hataları kapat
9
  logging.set_verbosity_error()
10
 
 
 
11
 
12
+ st.title('Chat with Me')
13
  model = load_model('text_classification_model.h5')
14
 
 
 
 
 
 
 
 
 
 
 
15
  # Chat history
16
+ if 'chat' not in st.session_state:
17
+ st.session_state.chat = model.start_chat(history=[])
18
 
19
+ # Input for user question
20
  soru = st.text_input('Sor:')
21
 
22
+ # Function to extract text parts from the response
23
+ def extract_text(response_parts):
24
+ return response_parts[0].text.strip() if response_parts[0].text else ""
 
 
 
 
 
25
 
26
+ # Handle "Sor" button click
27
  if st.button('Sor'):
28
  if soru:
29
+ response = st.session_state.chat.send_message(soru)
30
+ st.session_state.chat.history.append({'role': 'model', 'text': extract_text(response.parts)})
31
+ st.session_state.chat.history.append({'role': 'user', 'text': soru})
32
+ st.session_state.last_question = soru
33
+ st.session_state.last_response = extract_text(response.parts)
34
  st.experimental_rerun()
35
 
36
+ # Display chat history
37
+ for message in reversed(st.session_state.chat.history):
38
+ if message.role == 'user':
39
  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)
40
+ elif message.role == 'model':
41
  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)
42
 
43
+ # Handle "Yeni Sohbet" button click
44
  if st.button('Yeni Sohbet'):
45
+ st.session_state.chat = model.start_chat(history=[])
46
+ st.session_state.last_question = ''
47
+ st.session_state.last_response = ''
48
+ st.experimental_rerun()