Spaces:
Sleeping
Sleeping
Application File
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Create a function for getting a response from Gemini
|
| 6 |
+
|
| 7 |
+
def get_response(question):
|
| 8 |
+
try:
|
| 9 |
+
# Ensure the API key is correctly retrieved
|
| 10 |
+
api_key = os.getenv("AIzaSyCUOZsk6wPiJvDeHjfBAoqPo3sGjHFdsN0Y") # Use environment variable for API key
|
| 11 |
+
if not api_key:
|
| 12 |
+
raise ValueError("API key for Gemini is missing.")
|
| 13 |
+
|
| 14 |
+
# Configure API
|
| 15 |
+
genai.configure(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
# Define the generation configuration
|
| 18 |
+
generation_config = {
|
| 19 |
+
"temperature": 1,
|
| 20 |
+
"top_p": 0.95,
|
| 21 |
+
"top_k": 40,
|
| 22 |
+
"max_output_tokens": 8192,
|
| 23 |
+
"response_mime_type": "text/plain",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Initialize the model
|
| 27 |
+
model = genai.GenerativeModel(
|
| 28 |
+
model_name="gemini-1.5-pro-002",
|
| 29 |
+
generation_config=generation_config,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Start a chat session (can be empty or with history)
|
| 33 |
+
chat_session = model.start_chat(history=[])
|
| 34 |
+
|
| 35 |
+
# Send the message and get the response
|
| 36 |
+
response = chat_session.send_message(question)
|
| 37 |
+
|
| 38 |
+
# Ensure response is correctly parsed (adjust if needed based on response format)
|
| 39 |
+
return response.get("text", "No text response found.")
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# Handle any exceptions that occur during the process
|
| 43 |
+
return f"Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# Using Streamlit for generation of page
|
| 46 |
+
st.set_page_config(page_title="Ask GPT", page_icon=":robot:")
|
| 47 |
+
st.header("Ask GPT Application")
|
| 48 |
+
|
| 49 |
+
# Create function for taking user input
|
| 50 |
+
def get_user_input():
|
| 51 |
+
text = st.text_input("Ask:", key="input")
|
| 52 |
+
return text # Return the user input text
|
| 53 |
+
|
| 54 |
+
# Display the input and response after submission
|
| 55 |
+
user_input = get_user_input()
|
| 56 |
+
|
| 57 |
+
# Submit Button
|
| 58 |
+
submit = st.button("Ask")
|
| 59 |
+
|
| 60 |
+
if submit and user_input:
|
| 61 |
+
resp = get_response(user_input) # Call the get_response function
|
| 62 |
+
st.subheader("Answer: ")
|
| 63 |
+
st.write(resp) # Display the response
|