File size: 1,577 Bytes
e4d3289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3029342
 
 
 
e4d3289
 
 
 
 
 
 
 
3029342
e4d3289
3029342
 
 
 
 
 
 
 
 
e4d3289
 
 
 
3029342
 
 
 
 
 
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
import streamlit as st
import google.generativeai as genai

# Configure Gemini API
genai.configure(api_key='AIzaSyBDvXBds9i0TFZL6c7d7KACti6P1M7U_gc')
model = genai.GenerativeModel('gemini-pro')

def generate_response(user_message):
    try:
        response = model.generate_content(user_message)
        if response.text:
            return response.text
        else:
            return "Error: The model generated an empty response."
    except Exception as e:
        return f"An error occurred: {str(e)}"

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

st.title("Gemini QA System")
st.write("Ask a question and get an answer from Gemini AI.")

# Text input for user question
user_message = st.text_area("Enter your question here...", height=100)

# Button to submit the question
if st.button("Get Response"):
    # Generate response
    response = generate_response(user_message)
    
    # Add to chat history
    st.session_state.chat_history.append(("You", user_message))
    st.session_state.chat_history.append(("Gemini", response))

# Display chat history
st.write("### Chat History")
for role, message in st.session_state.chat_history:
    st.text_area(f"{role}:", value=message, height=100, disabled=True)

# Examples section
st.write("### Examples")
st.write("- What is the capital of France?")
st.write("- Explain quantum computing in simple terms.")

# Clear chat history button
if st.button("Clear Chat History"):
    st.session_state.chat_history = []
    st.experimental_rerun()