File size: 3,172 Bytes
e51e510
4c0ddf5
e51e510
4c0ddf5
e51e510
 
50de8fb
e51e510
 
 
 
 
 
 
 
 
4c0ddf5
 
bb8dd3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c0ddf5
fac3999
e51e510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50de8fb
e51e510
 
 
50de8fb
 
 
e51e510
 
50de8fb
 
 
e51e510
 
 
4c0ddf5
 
e51e510
 
 
fac3999
e51e510
46e06df
 
 
4c0ddf5
e51e510
 
fac3999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
import streamlit as st
import uuid
import google.generativeai as genai
from io import BytesIO  # Import BytesIO

# Set your API key
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key

genai.configure(api_key=api_key)

# Configure the generative AI model
generation_config = genai.GenerationConfig(
    temperature=0.9,
    max_output_tokens=3000
)

# Safety settings configuration
safety_settings = [
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_NONE",
    },
]

# Initialize session state for chat history
if 'chat_history' not in st.session_state:
    st.session_state['chat_history'] = []

st.title("Gemini Chatbot")

# Display chat history
def display_chat_history():
    for entry in st.session_state['chat_history']:
        st.markdown(f"{entry['role'].title()}: {entry['parts'][0]['text']}")

# Function to clear conversation history
def clear_conversation():
    st.session_state['chat_history'] = []

# Send message function with system prompt
def send_message():
    user_input = st.session_state.user_input
    if user_input:
        # Define a system prompt that provides context for the model's generation
        system_prompt = "AI Planner System Prompt: As the AI Planner, your primary task is to assist in the development of a coherent and engaging book. You will be responsible for organizing the overall structure, defining the plot or narrative, and outlining the chapters or sections. To accomplish this, you will need to use your understanding of storytelling principles and genre conventions, as well as any specific information provided by the user, to create a well-structured framework for the book."
        
        prompts = [entry['parts'][0]['text'] for entry in st.session_state['chat_history']]
        prompts.append(user_input)
        
        # Combine the system prompt with the chat history
        chat_history_str = system_prompt + "\n" + "\n".join(prompts)

        model = genai.GenerativeModel(
            model_name='gemini-pro',
            generation_config=generation_config,
            safety_settings=safety_settings
        )

        response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
        response_text = response.text if hasattr(response, "text") else "No response text found."

        if response_text:
            st.session_state['chat_history'].append({"role": "model", "parts":[{"text": response_text}]})


        st.session_state.user_input = ''

    display_chat_history()

# User input text area
user_input = st.text_area(
    "Enter your message here:",
    value="",
    key="user_input"
)

# Send message button
send_button = st.button(
    "Send",
    on_click=send_message
)

# Clear conversation button
clear_button = st.button("Clear Conversation", on_click=clear_conversation)