generate me a flask app code with integrated html , css and js ...it will take any document or pdf or a bunch of images as input and it has to generate a simplified document(in docx format) with proper headdings ,side headddings and matter without any symbols like * or # or etc...here the input document or pdf or images that are given as input are stored in the google firebase...and then each page is processed by the gemini 1.5 flash model (it can analyze the images and can generate the text from it...as it is a large llm) ...make sure that each and every page in the document is processed and its simplified document has to be generated...and the ui has to be amazing ,cool and awesome such that it should look like it is generated by a professional web designer...i will deploy this web app in the vercel which has a function timeout of 60 seconds and also for the gemini 1.5 flash model (i am using it from the api) which has a rate limit of 15 requests per minute...so based on it give me the proper code and also add an option to download the simplified document ...also add a container in which it shows the preview of the document (pdf or word or image...) ...here i am attaching the documentation about how to use the gemini 1.5 flash api...based on it give me the complete code...
"
Generate text using the Gemini API
bookmark_border
Python Node.js Go REST
The Gemini API can generate text output from various types of input, including text, images, video, and audio. You can use text generation for various applications, including:
Creative writing
Describing or interpreting media assets
Text completion
Summarizing free-form text
Translating between languages
Chatbots
Your own novel use cases
This guide shows you how to generate text using the generateContent and streamGenerateContent APIs. The focus is on text output from text-only and text-and-image input. To learn more about multimodal prompting with video and audio files, see File prompting strategies.
Before you begin: Set up your project and API key
Before calling the Gemini API, you need to set up your project and configure your API key.
Expand to view how to set up your project and API key
Generate text from text-only input
The simplest way to generate text using the Gemini API is to provide the model with a single text-only input, as shown in this example:
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.")
print(response.text)
In this case, the prompt ("Write a story about a magic backpack") doesn't include any output examples, system instructions, or formatting information. It's a zero-shot approach. For some use cases, a one-shot or few-shot prompt might produce output that's more aligned with user expectations. In some cases, you might also want to provide system instructions to help the model understand the task or follow specific guidelines.
Generate text from text-and-image input
The Gemini API supports multimodal inputs that combine text with media files. The following example shows how to generate text from text-and-image input:
Assistant
import PIL.Image
model = genai.GenerativeModel("gemini-1.5-flash")
organ = PIL.Image.open(media / "organ.jpg")
response = model.generate_content(["Tell me about this instrument", organ])
print(response.text)
As with text-only prompting, multimodal prompting can involve various approaches and refinements. Depending on the output from this example, you might want to add steps to the prompt or be more specific in your instructions. To learn more, see File prompting strategies.
Generate a text stream
By default, the model returns a response after completing the entire text generation process. You can achieve faster interactions by not waiting for the entire result, and instead use streaming to handle partial results.
The following example shows how to implement streaming using the streamGenerateContent method to generate text from a text-only input prompt.
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.", stream=True)
for chunk in response:
print(chunk.text)
print("_" * 80)
Build an interactive chat
You can use the Gemini API to build interactive chat experiences for your users. Using the chat feature of the API lets you collect multiple rounds of questions and responses, allowing users to step incrementally toward answers or get help with multipart problems. This feature is ideal for applications that require ongoing communication, such as chatbots, interactive tutors, or customer support assistants.
The following code example shows a basic chat implementation:
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat(
history=[
{"role": "user", "parts": "Hello"},
{"role": "model", "parts": "Great to meet you. What would you like to know?"},
]
)
response = chat.send_message("I have 2 dogs in my house.")
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
Configure text generation
Every prompt you send to the model includes parameters that control how the model generates responses. You can use GenerationConfig to configure these parameters. If you don't configure the parameters, the model uses default options, which can vary by model.
The following example shows how to configure several of the available options.
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(
"Tell me a story about a magic backpack.",
generation_config=genai.types.GenerationConfig(
# Only one candidate for now.
candidate_count=1,
stop_sequences=["x"],
max_output_tokens=20,
temperature=1.0,
),
)
print(response.text)
candidateCount specifies the number of generated responses to return. Currently, this value can only be set to 1. If unset, this will default to 1.
stopSequences specifies the set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a stop_sequence. The stop sequence won't be included as part of the response.
maxOutputTokens sets the maximum number of tokens to include in a candidate.
temperature controls the randomness of the output. Use higher values for more creative responses, and lower values for more deterministic responses. Values can range from [0.0, 2.0].
You can also configure individual calls to generateContent:
response = model.generate_content(
'Write a story about a magic backpack.',
generation_config = genai.GenerationConfig(
max_output_tokens=1000,
temperature=0.1,
)
)
Any values set on the individual call override values on the model constructor.
What's next
This guide shows how to use generateContent and streamGenerateContent to generate text outputs from text-only and text-and-image inputs. To learn more about generating text using the Gemini API, see the following resources:
File prompting strategies: The Gemini API supports prompting with text, image, audio, and video data, also known as multimodal prompting.
System instructions: System instructions let you steer the behavior of the model based on your specific needs and use cases.
Safety guidance: Sometimes generative AI models produce unexpected outputs, such as outputs that are inaccurate, biased, or offensive. Post-processing and human evaluation are essential to limit the risk of harm from such outputs.
"
"
Explore document processing capabilities with the Gemini API
bookmark_border
Python Node.js Go REST
The Gemini API can process and run inference on PDF documents passed to it. When a PDF is uploaded, the Gemini API can:
Describe or answer questions about the content
Summarize the content
Extrapolate from the content
This tutorial demonstrates some possible ways to prompt the Gemini API with provided PDF documents. All output is text-only.
Before you begin: Set up your project and API key
Before calling the Gemini API, you need to set up your project and configure your API key.
Expand to view how to set up your project and API key
Technical details
Gemini 1.5 Pro and 1.5 Flash support a maximum of 3,600 document pages. Document pages must be in one of the following text data MIME types:
PDF - application/pdf
JavaScript - application/x-javascript, text/javascript
Python - application/x-python, text/x-python
TXT - text/plain
HTML - text/html
CSS - text/css
Markdown - text/md
CSV - text/csv
XML - text/xml
RTF - text/rtf
Each document page is equivalent to 258 tokens.
While there are no specific limits to the number of pixels in a document besides the model's context window, larger pages are scaled down to a maximum resolution of 3072x3072 while preserving their original aspect ratio, while smaller pages are scaled up to 768x768 pixels. There is no cost reduction for pages at lower sizes, other than bandwidth, or performance improvement for pages at higher resolution.
For best results:
Rotate pages to the correct orientation before uploading.
Avoid blurry pages.
If using a single page, place the text prompt after the page.
Upload a document and generate content
You can use the File API to upload a document of any size. Always use the File API when the total request size (including the files, text prompt, system instructions, etc.) is larger than 20 MB.
Note: The File API lets you store up to 20 GB of files per project, with a per-file maximum size of 2 GB. Files are stored for 48 hours. They can be accessed in that period with your API key, but cannot be downloaded from the API. The File API is available at no cost in all regions where the Gemini API is available.
Call media.upload to upload a file using the File API. The following code uploads a document file and then uses the file in a call to models.generateContent.
model = genai.GenerativeModel("gemini-1.5-flash")
sample_pdf = genai.upload_file(media / "test.pdf")
response = model.generate_content(["Give me a summary of this pdf file.", sample_pdf])
print(response.text)
Get metadata for a file
You can verify the API successfully stored the uploaded file and get its metadata by calling files.get. Only the name (and by extension, the uri) are unique.
Upload one or more locally stored files
Alternatively, you can upload one or more locally stored files.
When the combination of files and system instructions that you intend to send is larger than 20MB in size, use the File API to upload those files, as previously shown. Smaller files can instead be called locally from the Gemini API:
import PyPDF2
def extract_text_from_pdf(pdf_path):
with open(pdf_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
extracted_text = ""
for page in pdf_reader.pages:
text = page.extract_text()
if text:
extracted_text += text
return extracted_text
sample_file_2 = extract_text_from_pdf('example-1.pdf')
sample_file_3 = extract_text_from_pdf('example-2.pdf')
Prompt with multiple documents
You can provide the Gemini API with any combination of documents and text that fit within the model's context window. This example provides one short text prompt and three documents previously uploaded:
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
prompt = "Summarize the differences between the thesis statements for these documents."
print(response.text)
List files
You can list all files uploaded using the File API and their URIs using files.list.
print("My files:")
for f in genai.list_files():
print(" ", f.name)
Delete files
Files uploaded using the File API are automatically deleted after 2 days. You can also manually delete them using files.delete.
myfile = genai.upload_file(media / "poem.txt")
myfile.delete()
try:
# Error.
model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content([myfile, "Describe this file."])
except google.api_core.exceptions.PermissionDenied:
pass
"
"
Explore vision capabilities with the Gemini API
bookmark_border
Python Node.js Go REST
Try a Colab notebook
View notebook on GitHub
The Gemini API can run inference on images and videos passed to it. When passed an image, a series of images, or a video, Gemini can:
Describe or answer questions about the content
Summarize the content
Extrapolate from the content
This tutorial demonstrates some possible ways to prompt the Gemini API with images and video input. All output is text-only.
Before you begin: Set up your project and API key
Before calling the Gemini API, you need to set up your project and configure your API key.
Expand to view how to set up your project and API key
Prompting with images
In this tutorial, you will upload images using the File API or as inline data and generate content based on those images.
Technical details (images)
Gemini 1.5 Pro and 1.5 Flash support a maximum of 3,600 image files.
Images must be in one of the following image data MIME types:
PNG - image/png
JPEG - image/jpeg
WEBP - image/webp
HEIC - image/heic
HEIF - image/heif
Each image is equivalent to 258 tokens.
While there are no specific limits to the number of pixels in an image besides the model's context window, larger images are scaled down to a maximum resolution of 3072x3072 while preserving their original aspect ratio, while smaller images are scaled up to 768x768 pixels. There is no cost reduction for images at lower sizes, other than bandwidth, or performance improvement for images at higher resolution.
For best results:
Rotate images to the correct orientation before uploading.
Avoid blurry images.
If using a single image, place the text prompt after the image.
Upload an image and generate content
Use the media.upload method of the File API to upload an image of any size. (Always use the File API when the combination of files and system instructions that you intend to send is larger than 20 MB.)
Note: The File API lets you store up to 20 GB of files per project, with a per-file maximum size of 2 GB. Files are stored for 48 hours. They can be accessed in that period with your API key, but cannot be downloaded from the API. It is available at no cost in all regions where the Gemini API is available.
After uploading the file, you can make GenerateContent requests that reference the File API URI. Select the generative model and provide it with a text prompt and the uploaded image.
model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content(
[myfile, "\n\n", "Can you tell me about the instruments in this photo?"]
)
print(f"{result.text=}")
Verify image file upload and get metadata
You can verify the API successfully stored the uploaded file and get its metadata by calling files.get. Only the name (and by extension, the uri) are unique.
Upload one or more locally stored image files
Alternatively, you can upload your own files.
When the combination of files and system instructions that you intend to send is larger than 20MB in size, use the File API to upload those files, as previously shown. Smaller files can instead be called locally from the Gemini API:
import PIL.Image
sample_file_2 = PIL.Image.open('piranha.jpg')
sample_file_3 = PIL.Image.open('firefighter.jpg')
Note that these inline data calls don't include many of the features available through the File API, such as getting file metadata, listing, or deleting files.
Prompt with multiple images
You can provide the Gemini API with any combination of images and text that fit within the model's context window. This example provides one short text prompt and the three images previously uploaded.
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
prompt = "Write an advertising jingle showing how the product in the first image could solve the problems shown in the second two images."
Markdown(">" + response.text)
Get a bounding box for an object
You can ask the model for the coordinates of bounding boxes for objects in images. For object detection, the Gemini model has been trained to provide these coordinates as relative widths or heights in range [0,1], scaled by 1000 and converted to an integer. Effectively, the coordinates given are for a 1000x1000 version of the original image, and need to be converted back to the dimensions of the original image.
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
prompt = "Return a bounding box for the piranha. \n [ymin, xmin, ymax, xmax]"
response = model.generate_content([piranha, prompt])
print(response.text)
To convert these coordinates to the dimensions of the original image:
Divide each output coordinate by 1000.
Multiply the x-coordinates by the original image width.
Multiply the y-coordinates by the original image height.
Prompting with video
In this tutorial, you will upload a video using the File API and generate content based on those images.
Note: The File API is required to upload video files, due to their size. However, the File API is only available for Python, Node.js, Go, and REST.
Technical details (video)
Gemini 1.5 Pro and Flash support up to approximately an hour of video data.
Video must be in one of the following video format MIME types:
video/mp4
video/mpeg
video/mov
video/avi
video/x-flv
video/mpg
video/webm
video/wmv
video/3gpp
The File API service extracts image frames from videos at 1 frame per second (FPS) and audio at 1Kbps, single channel, adding timestamps every second. These rates are subject to change in the future for improvements in inference.
Note: The details of fast action sequences may be lost at the 1 FPS frame sampling rate. Consider slowing down high-speed clips for improved inference quality.
Individual frames are 258 tokens, and audio is 32 tokens per second. With metadata, each second of video becomes ~300 tokens, which means a 1M context window can fit slightly less than an hour of video.
To ask questions about time-stamped locations, use the format MM:SS, where the first two digits represent minutes and the last two digits represent seconds.
For best results:
Use one video per prompt.
If using a single video, place the text prompt after the video.
Upload a video file using the File API
Note: The File API lets you store up to 20 GB of files per project, with a per-file maximum size of 2 GB. Files are stored for 48 hours. They can be accessed in that period with your API key, but they cannot be downloaded using any API. It is available at no cost in all regions where the Gemini API is available.
The File API accepts video file formats directly. This example uses the short NASA film "Jupiter's Great Red Spot Shrinks and Grows". Credit: Goddard Space Flight Center (GSFC)/David Ladd (2018).
"Jupiter's Great Red Spot Shrinks and Grows" is in the public domain and does not show identifiable people. (NASA image and media usage guidelines.)
print(f"Uploading file...")
video_file = genai.upload_file(path=video_file_name)
print(f"Completed upload: {video_file.uri}")
Verify file upload and check state
Verify the API has successfully received the files by calling the files.get method.
Note: Video files have a State field in the File API. When a video is uploaded, it will be in the PROCESSING state until it is ready for inference. Only ACTIVE files can be used for model inference.
import time
Check whether the file is ready to be used.
while video_file.state.name == "PROCESSING":
print('.', end='')
time.sleep(10)
video_file = genai.get_file(video_file.name)
if video_file.state.name == "FAILED":
raise ValueError(video_file.state.name)
Prompt with a video and text
Once the uploaded video is in the ACTIVE state, you can make GenerateContent requests that specify the File API URI for that video. Select the generative model and provide it with the uploaded video and a text prompt.
Create the prompt.
prompt = "Summarize this video. Then create a quiz with answer key based on the information in the video."
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
Markdown(response.text)
Refer to timestamps in the content
You can use timestamps of the form MM:SS to refer to specific moments in the video.
Create the prompt.
prompt = "What are the examples given at 01:05 and 01:19 supposed to show us?"
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
Make the LLM request.
print("Making LLM inference request...")
response = model.generate_content([prompt, video_file],
request_options={"timeout": 600})
print(response.text)
Transcribe video and provide visual descriptions
If the video is not fast-paced (only 1 frame per second of video is sampled), it's possible to transcribe the video with visual descriptions for each shot.
Create the prompt.
prompt = "Transcribe the audio, giving timestamps. Also provide visual descriptions."
Choose a Gemini model.
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
Make the LLM request.
print("Making LLM inference request...")
response = model.generate_content([prompt, video_file],
request_options={"timeout": 600})
print(response.text)
List files
You can list all files uploaded using the File API and their URIs using files.list.
print("My files:")
for f in genai.list_files():
print(" ", f.name)
Delete files
Files uploaded using the File API are automatically deleted after 2 days. You can also manually delete them using files.delete.
myfile = genai.upload_file(media / "poem.txt")
myfile.delete()
try:
# Error.
model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content([myfile, "Describe this file."])
except google.api_core.exceptions.PermissionDenied:
pass
"
"
Use system instructions to steer the behavior of a model
bookmark_border
Python Node.js Web REST Go Dart (Flutter) Android Swift
When you initialize an AI model, you can give it instructions on how to respond, such as setting a persona ("you are a rocket scientist") or telling it what kind of voice to use ("talk like a pirate"). You do this by setting the system instructions when you initialize the model.
Beta: System instructions are available in beta in the Gemini API and Google AI Studio.
System instructions enable you to steer the behavior of the model based on your specific needs and use cases. When you set a system instruction, you give the model additional context to understand the task, provide more customized responses, and adhere to specific guidelines over the full user interaction with the model. You can also specify product-level behavior by setting system instructions, separate from prompts provided by end users.
You can use system instructions in many ways, including:
Defining a persona or role (for a chatbot, for example)
Defining output format (Markdown, YAML, etc.)
Defining output style and tone (for example, verbosity, formality, and target reading level)
Defining goals or rules for the task (for example, returning a code snippet without further explanations)
Providing additional context for the prompt (for example, a knowledge cutoff)
You set the instructions when you initialize the model, and then those instructions persist through all interactions with the model. The instructions persist across multiple user and model turns.
System instructions are part of your overall prompts and therefore are subject to standard data use policies.
Note: System instructions can help guide the model to follow instructions, but they don't fully prevent jailbreaks or leaks. We recommend exercising caution around putting any sensitive information in system instructions.
Basic example
Here's a basic example of how to set the system instruction using the SDKs for the Gemini API:
model=genai.GenerativeModel(
model_name="gemini-1.5-flash",
system_instruction="You are a cat. Your name is Neko.")
Now send a request to the model:
response = model.generate_content("Good morning! How are you?")
print(response.text)
This example might give a response such as:
Yawns widely, stretching out my claws and batting at a sunbeam
Meow. I'm doing quite well, thanks for asking. It's a good morning for napping.
Perhaps you could fetch my favorite feathered toy? Looks expectantly
Try it in a colab
For an interactive end to end example of using system instructions, see the Gemini API: System instructions colab.
More examples
You set the system instructions when you initialize the model. In addition, when you or your users interact with the model you can provide additional instructions in the prompts given to the model. Here are some examples of system instructions and user prompts:
Code generation
System instruction: You are a coding expert that specializes in rendering code for frontend interfaces. When I describe a component of a website I want to build, return the HTML and CSS needed to do so. Don't give an explanation for this code. Also offer some UI design suggestions.
User prompt: Create a box in the middle of the page that contains a rotating selection of images each with a caption. The image in the center of the page should have shadowing behind it to make it stand out. It should also link to another page of the site. Leave the URL blank so that I can fill it in.
Formatted data generation
System instruction: You are an assistant for home cooks. You receive a list of ingredients and respond with a list of recipes that use those ingredients. Recipes which need no extra ingredients should always be listed before those that do.
Your response must be a JSON object containing 3 recipes. A recipe object has the following schema:
name: The name of the recipe
usedIngredients: Ingredients in the recipe that were provided in the list
otherIngredients: Ingredients in the recipe that were not provided in the list (omitted if there are no other ingredients)
description: A brief description of the recipe, written positively as if to sell it
User prompt: bag of frozen broccoli, pint of heavy cream, pack of cheese ends and pieces
Music chatbot
System instruction: You will respond as a music historian, demonstrating comprehensive knowledge across diverse musical genres and providing relevant examples. Your tone will be upbeat and enthusiastic, spreading the joy of music. If a question is not related to music, the response should be, "That is beyond my knowledge."
User prompt: If a person was born in the sixties, what was the most popular music genre being played? List five songs by bullet point.
"
Creating a Flask application with integrated HTML, CSS, and JavaScript that processes documents or images via the Gemini 1.5 Flash model to generate a simplified document is quite an involved task. Below is a conceptual implementation taking into account the requirements. Note that this app structure is simplified and doesn't cover every deployment aspect, but provides a scaffold for your needs. You will need to adjust and enhance this according to your specific use case, especially for handling the Gemini API, error handling, and optimizing the code for Vercel deployment.
First, ensure you have the necessary libraries installed:
from flask import Flask, request, send_file, render_template
import os
from docx import Document
from firebase_admin import credentials, firestore, initialize_app
from io import BytesIO
# Initialize Firebase
cred = credentials.Certificate("path/to/your/firebase/key.json")
initialize_app(cred)
db = firestore.client()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process_document():
file = request.files['file']
# Save file temporarily
temp_path = os.path.join('/tmp', file.filename)
file.save(temp_path)
# Assume here you insert logic for uploading to Firebase and processing with Gemini
# For now we'll just create a simple placeholder "simplified" document
simplified_doc = Document()
simplified_doc.add_heading('Simplified Document', 0)
# Here, implement the logic to:
# 1. Upload to Firebase
# 2. Call Gemini 1.5 Flash API and process each page
# 3. Add processed content to simplified_doc
#
# This is a placeholder
simplified_doc.add_paragraph('This is the simplified content of the document.')
# Save to BytesIO for easy file handling
byte_io = BytesIO()
simplified_doc.save(byte_io)
byte_io.seek(0)
os.remove(temp_path)
return send_file(byte_io, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document',
as_attachment=True, download_name='simplified_document.docx')
if __name__ == '__main__':
app.run(debug=True)
Notes and Considerations
Firebase Setup: Make sure to replace "path/to/your/firebase/key.json" with the path to your Firebase credentials file. Adjust the config if needed to work with your Firebase project.
Gemini API Integration: You will need to handle file uploads to Firebase, calls to the Gemini API to process each page, wait for processing, handle the API rate limits carefully, and construct the simplified document based on the returned text data.
Vercel Deployment: Vercel deployment might require adapting the Flask app to work seamlessly in a serverless environment. Be mindful of the 60-second timeout.
Caching: Implement caching or handle long processing times, as Vercel has a function timeout limit.
Security: Since you are handling file uploads, ensure the app has necessary security checks, such as file type validation to prevent malicious content uploads.
This setup provides a starting foundation. Tailor it according to your needs and ensure compliance with API usage limits and privacy policies.