Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
# Page config
|
6 |
+
st.set_page_config(page_title="Pattern Completion", page_icon="🤖")
|
7 |
+
|
8 |
+
# Initialize session state for history if it doesn't exist
|
9 |
+
if 'history' not in st.session_state:
|
10 |
+
st.session_state.history = [
|
11 |
+
"input: dogs",
|
12 |
+
"output: cats",
|
13 |
+
"input: apple",
|
14 |
+
"output: samsung",
|
15 |
+
"input: b",
|
16 |
+
"output: c",
|
17 |
+
"input: friend",
|
18 |
+
"output: enemy",
|
19 |
+
"input: oil",
|
20 |
+
"output: water",
|
21 |
+
]
|
22 |
+
|
23 |
+
# Configure Gemini
|
24 |
+
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
|
25 |
+
|
26 |
+
# Create the model
|
27 |
+
generation_config = {
|
28 |
+
"temperature": 1,
|
29 |
+
"top_p": 0.95,
|
30 |
+
"top_k": 64,
|
31 |
+
"max_output_tokens": 8192,
|
32 |
+
"response_mime_type": "text/plain",
|
33 |
+
}
|
34 |
+
|
35 |
+
model = genai.GenerativeModel(
|
36 |
+
model_name="gemini-exp-1206",
|
37 |
+
generation_config=generation_config,
|
38 |
+
)
|
39 |
+
|
40 |
+
# Create the Streamlit interface
|
41 |
+
st.title("Pattern Completion")
|
42 |
+
|
43 |
+
# Input text box
|
44 |
+
user_input = st.text_input("Enter your input:")
|
45 |
+
|
46 |
+
if st.button("Generate"):
|
47 |
+
if user_input:
|
48 |
+
# Create prompt with history and new input
|
49 |
+
prompt = st.session_state.history.copy()
|
50 |
+
prompt.extend([f"input: {user_input}", "output: "])
|
51 |
+
|
52 |
+
# Generate response
|
53 |
+
try:
|
54 |
+
response = model.generate_content(prompt)
|
55 |
+
output = response.text.strip()
|
56 |
+
|
57 |
+
# Update history
|
58 |
+
st.session_state.history.extend([f"input: {user_input}", f"output: {output}"])
|
59 |
+
|
60 |
+
# Display result
|
61 |
+
st.success(f"Output: {output}")
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"An error occurred: {str(e)}")
|