Spaces:
Sleeping
Sleeping
#!/usr/bin/env python | |
# coding: utf-8 | |
import streamlit as st | |
import google.generativeai as genai | |
from transformers import logging | |
# Hugging Face Transformers kütüphanesinden gelen hataları kapat | |
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') | |
# Chat history | |
if 'chat' not in st.session_state: | |
st.session_state.chat = model.start_chat(history=[]) | |
# Input for user question | |
soru = st.text_input('Sor:') | |
# Function to extract text parts from the response | |
def extract_text(response_parts): | |
return response_parts[0].text.strip() if response_parts[0].text else "" | |
# Handle "Sor" button click | |
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() | |
# Display chat history | |
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) | |
# Handle "Yeni Sohbet" button click | |
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() |