Rooobert commited on
Commit
2f922ac
1 Parent(s): 1d23d9e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+
4
+ # Configure Gemini API
5
+ genai.configure(api_key='AIzaSyBIPYXqIvI1X6unKTbuWqcVfNFCePr8_sQ')
6
+ model = genai.GenerativeModel('gemini-pro')
7
+
8
+ def generate_response(user_message):
9
+ try:
10
+ # Use Gemini API to generate a response
11
+ response = model.generate_content(user_message)
12
+
13
+ # Check if the response has text
14
+ if response.text:
15
+ return response.text
16
+ else:
17
+ return "Error: The model generated an empty response."
18
+ except Exception as e:
19
+ return f"An error occurred: {str(e)}"
20
+
21
+ # Initialize session state for conversation history
22
+ if 'conversation' not in st.session_state:
23
+ st.session_state.conversation = []
24
+
25
+ st.title("🦷🦷Gemini QA System🦷🦷")
26
+ st.write("Ask a question and get an answer from Gemini AI.")
27
+
28
+ # User input
29
+ user_message = st.text_input("Enter your question here...")
30
+
31
+ if st.button("Submit"):
32
+ if user_message:
33
+ response = generate_response(user_message)
34
+ st.session_state.conversation.append((user_message, response))
35
+
36
+ # Display conversation history
37
+ if st.session_state.conversation:
38
+ st.write("### Conversation History")
39
+ for i, (question, answer) in enumerate(st.session_state.conversation):
40
+ st.write(f"**Q{i+1}:** {question}")
41
+ st.write(f"**A{i+1}:** {answer}")
42
+
43
+ # Examples in the sidebar
44
+ st.sidebar.title("Examples")
45
+ st.sidebar.write("Click on an example to use it:")
46
+ examples = ["What is the capital of France?", "Explain quantum computing in simple terms."]
47
+ for example in examples:
48
+ if st.sidebar.button(example):
49
+ response = generate_response(example)
50
+ st.session_state.conversation.append((example, response))