yunuseduran commited on
Commit
f79271c
·
verified ·
1 Parent(s): 0974ac3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +49 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
11
+ genai.configure(api_key=os.getenv("key"))
12
+
13
+ st.title('ask me a question')
14
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
15
+
16
+ # Chat history
17
+ if 'chat' not in st.session_state:
18
+ st.session_state.chat = model.start_chat(history=[])
19
+
20
+ # Input for user question
21
+ soru = st.text_input('Sor:')
22
+
23
+ # Function to extract text parts from the response
24
+ def extract_text(response_parts):
25
+ return response_parts[0].text.strip() if response_parts[0].text else ""
26
+
27
+ # Handle "Sor" button click
28
+ if st.button('Ask'):
29
+ if soru:
30
+ response = st.session_state.chat.send_message(soru)
31
+ st.session_state.chat.history.append({'role': 'model', 'text': extract_text(response.parts)})
32
+ st.session_state.chat.history.append({'role': 'user', 'text': soru})
33
+ st.session_state.last_question = soru
34
+ st.session_state.last_response = extract_text(response.parts)
35
+ st.experimental_rerun()
36
+
37
+ # Display chat history
38
+ for message in reversed(st.session_state.chat.history):
39
+ if message['role'] == 'user':
40
+ 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)
41
+ elif message['role'] == 'model':
42
+ 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)
43
+
44
+ # Handle "Yeni Sohbet" button click
45
+ if st.button('Yeni Sohbet'):
46
+ st.session_state.chat = model.start_chat(history=[])
47
+ st.session_state.last_question = ''
48
+ st.session_state.last_response = ''
49
+ st.experimental_rerun()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ transformers