Skip to content

Commit

Permalink
Add a few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
logankilpatrick committed May 7, 2024
1 parent 76191bf commit 2267a76
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@
import google.generativeai as genai

# WARNING: Do not share code with you API key hard coded in it.
# Get your Gemini API key from: https://aistudio.google.com/app/apikey
GOOGLE_API_KEY=""
genai.configure(api_key=GOOGLE_API_KEY)

# The rate limits are low on this model, so you might need to switch to `gemini-pro`
model = genai.GenerativeModel('gemini-1.5-pro-latest')

app = Flask(__name__)
ALLOWED_EXTENSIONS = {"txt", "pdf", "png", "jpg", "jpeg", "gif", "csv"}
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}

chat_session = model.start_chat(history=[])
next_message = ""
next_image = ""

def allowed_file(filename):
"""Returns if a filename is supported via its extension"""
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/upload", methods=["POST"])
def upload_file():
"""Takes in a file, checks if it is valid, and saves it for the next request to the API"""
global next_image

if "file" not in request.files:
Expand All @@ -57,10 +60,12 @@ def upload_file():

@app.route("/", methods=["GET"])
def index():
"""Renders the main homepage for the app"""
return render_template("index.html", chat_history=chat_session.history)

@app.route("/chat", methods=["POST"])
def chat():
"""Takes in the message the user wants to send to the Gemini API, saves it"""
global next_message
next_message = request.json["message"]
print(chat_session.history)
Expand All @@ -69,12 +74,14 @@ def chat():

@app.route("/stream", methods=["GET"])
def stream():
"""Streams the response from the serve for both multi-modal and plain text requests"""
def generate():
global next_message
global next_image
assistant_response_content = ""

if next_image != "":
# This only works with `gemini-1.5-pro-latest`
response = chat_session.send_message([next_message, next_image], stream=True)
next_image = ""
else:
Expand Down

0 comments on commit 2267a76

Please sign in to comment.