|
|
|
|
|
|
|
import streamlit as st |
|
import google.generativeai as genai |
|
from transformers import logging |
|
|
|
|
|
logging.set_verbosity_error() |
|
import os |
|
genai.configure(api_key=os.getenv("key")) |
|
|
|
st.title('Chat with Me') |
|
model = genai.GenerativeModel('gemini-1.5-pro-latest') |
|
|
|
|
|
if 'chat' not in st.session_state: |
|
st.session_state.chat = model.start_chat(history=[]) |
|
|
|
|
|
soru = st.text_input('Sor:') |
|
|
|
|
|
def extract_text(response_parts): |
|
return response_parts[0].text.strip() if response_parts[0].text else "" |
|
|
|
|
|
if st.button('Sor'): |
|
if soru: |
|
response = st.session_state.chat.send_message(soru) |
|
st.session_state.chat.history.append({'role': 'model', 'text': extract_text(response.parts)}) |
|
st.session_state.chat.history.append({'role': 'user', 'text': soru}) |
|
st.session_state.last_question = soru |
|
st.session_state.last_response = extract_text(response.parts) |
|
st.experimental_rerun() |
|
|
|
|
|
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) |
|
|
|
|
|
if st.button('Yeni Sohbet'): |
|
st.session_state.chat = model.start_chat(history=[]) |
|
st.session_state.last_question = '' |
|
st.session_state.last_response = '' |
|
st.experimental_rerun() |
|
|