Spaces:
Sleeping
Sleeping
| 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() |