Update app.py
Browse files
app.py
CHANGED
@@ -9,56 +9,62 @@ st.set_page_config(page_title="Pattern Completion", page_icon="🤖")
|
|
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 |
-
#
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
)
|
39 |
|
40 |
-
# Create the
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
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 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Create the Streamlit interface
|
24 |
+
st.title("Pattern Completion")
|
25 |
|
26 |
+
# API key input
|
27 |
+
api_key = st.text_input("Enter your Gemini API key:", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
if api_key:
|
30 |
+
# Configure Gemini
|
31 |
+
genai.configure(api_key=api_key)
|
|
|
32 |
|
33 |
+
# Create the model
|
34 |
+
generation_config = {
|
35 |
+
"temperature": 1,
|
36 |
+
"top_p": 0.95,
|
37 |
+
"top_k": 64,
|
38 |
+
"max_output_tokens": 8192,
|
39 |
+
"response_mime_type": "text/plain",
|
40 |
+
}
|
41 |
|
42 |
+
model = genai.GenerativeModel(
|
43 |
+
model_name="gemini-exp-1206",
|
44 |
+
generation_config=generation_config,
|
45 |
+
)
|
46 |
|
47 |
+
# Input text box
|
48 |
+
user_input = st.text_input("Enter your input:")
|
49 |
+
|
50 |
+
if st.button("Generate"):
|
51 |
+
if user_input:
|
52 |
+
# Create prompt with history and new input
|
53 |
+
prompt = st.session_state.history.copy()
|
54 |
+
prompt.extend([f"input: {user_input}", "output: "])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
# Generate response
|
57 |
+
try:
|
58 |
+
response = model.generate_content(prompt)
|
59 |
+
output = response.text.strip()
|
60 |
+
|
61 |
+
# Update history
|
62 |
+
st.session_state.history.extend([f"input: {user_input}", f"output: {output}"])
|
63 |
+
|
64 |
+
# Display result
|
65 |
+
st.success(f"Output: {output}")
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"An error occurred: {str(e)}")
|
69 |
+
else:
|
70 |
+
st.warning("Please enter your Gemini API key to use the app")
|