File size: 2,270 Bytes
b59f787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f79271c
 
 
 
 
 
 
 
 
 
 
 
b59f787
f79271c
 
 
 
 
 
 
 
 
 
 
 
 
 
b59f787
f79271c
 
 
 
 
 
 
 
 
b59f787
8cb8cde
f79271c
8cb8cde
f79271c
 
 
 
 
 
 
b59f787
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Hugging Face's logo
Hugging Face
Search models, datasets, users...
Models
Datasets
Spaces
Posts
Docs
Solutions
Pricing

Log In
Sign Up
Spaces:

Erayss
/
chat_me


like
0
App
Files
Community
chat_me
/
app.py

Erayss's picture
Erayss
Update app.py
f3be7f2
VERIFIED
8 days ago
raw
history
blame
contribute
delete
No virus
1.94 kB
#!/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()