Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
|
| 6 |
+
# Configure the Gemini API key
|
| 7 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 8 |
+
|
| 9 |
+
def upload_to_gemini(path, mime_type=None):
|
| 10 |
+
"""Uploads the given file to Gemini."""
|
| 11 |
+
file = genai.upload_file(path, mime_type=mime_type)
|
| 12 |
+
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
| 13 |
+
return file
|
| 14 |
+
|
| 15 |
+
def wait_for_files_active(files):
|
| 16 |
+
"""Waits for the given files to be active."""
|
| 17 |
+
print("Waiting for file processing...")
|
| 18 |
+
for name in (file.name for file in files):
|
| 19 |
+
file = genai.get_file(name)
|
| 20 |
+
while file.state.name == "PROCESSING":
|
| 21 |
+
print(".", end="", flush=True)
|
| 22 |
+
time.sleep(10)
|
| 23 |
+
file = genai.get_file(name)
|
| 24 |
+
if file.state.name != "ACTIVE":
|
| 25 |
+
raise Exception(f"File {file.name} failed to process")
|
| 26 |
+
print("...all files ready")
|
| 27 |
+
print()
|
| 28 |
+
|
| 29 |
+
# Initialize the model
|
| 30 |
+
generation_config = {
|
| 31 |
+
"temperature": 1,
|
| 32 |
+
"top_p": 0.95,
|
| 33 |
+
"top_k": 40,
|
| 34 |
+
"max_output_tokens": 8192,
|
| 35 |
+
"response_mime_type": "text/plain",
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
model = genai.GenerativeModel(
|
| 39 |
+
model_name="gemini-2.0-flash-exp",
|
| 40 |
+
generation_config=generation_config,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# File upload and processing
|
| 44 |
+
def process_files(files, user_prompt):
|
| 45 |
+
uploaded_files = [
|
| 46 |
+
upload_to_gemini(file.name, mime_type=file.type) for file in files
|
| 47 |
+
]
|
| 48 |
+
wait_for_files_active(uploaded_files)
|
| 49 |
+
|
| 50 |
+
# Create a chat session
|
| 51 |
+
chat_session = model.start_chat(
|
| 52 |
+
history=[
|
| 53 |
+
{"role": "user", "parts": [uploaded_files[0], user_prompt]},
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Send the user's message and get a response
|
| 58 |
+
response = chat_session.send_message(user_prompt)
|
| 59 |
+
return response.text
|
| 60 |
+
|
| 61 |
+
# Gradio interface
|
| 62 |
+
def chat_with_model(files, user_prompt):
|
| 63 |
+
return process_files(files, user_prompt)
|
| 64 |
+
|
| 65 |
+
# Create the Gradio UI
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
with gr.Row():
|
| 68 |
+
gr.Markdown("## Multimodal Chat with Gemini AI")
|
| 69 |
+
with gr.Row():
|
| 70 |
+
file_input = gr.File(label="Upload Files", file_types=["*"], file_count="multiple")
|
| 71 |
+
text_input = gr.Textbox(label="Enter your prompt")
|
| 72 |
+
with gr.Row():
|
| 73 |
+
submit_button = gr.Button("Submit")
|
| 74 |
+
with gr.Row():
|
| 75 |
+
output = gr.Textbox(label="Response from Gemini AI")
|
| 76 |
+
|
| 77 |
+
submit_button.click(chat_with_model, inputs=[file_input, text_input], outputs=output)
|
| 78 |
+
|
| 79 |
+
# Launch the Gradio app
|
| 80 |
+
demo.launch()
|