JeCabrera commited on
Commit
b76756f
verified
1 Parent(s): 4db85d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -14
app.py CHANGED
@@ -3,15 +3,21 @@ import streamlit as st
3
  import os
4
  import google.generativeai as genai
5
  from PIL import Image
 
6
 
7
  load_dotenv()
8
 
 
9
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
 
11
- # Function to get response from Gemini model
12
- def get_gemini_response(input_prompt, image_data, genre, length, language, mood):
13
- # Prepare the prompts
 
 
 
14
  if image_data:
 
15
  full_prompt = f"""
16
  You are a famous creative writer. Look at the image provided and create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the image and contain realistic and emotional elements.
17
  """
@@ -24,12 +30,36 @@ def get_gemini_response(input_prompt, image_data, genre, length, language, mood)
24
  Make sure it contains realistic and emotional elements.
25
  """
26
 
27
- # Call the API (the method might differ; check the documentation)
28
- response = genai.generate([full_prompt]) # Cambiar aqu铆 si es necesario seg煤n la documentaci贸n
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Check if response is valid and return text
31
- if response and response[0]: # Ajusta el 铆ndice seg煤n la respuesta de la API
32
- return response[0].text
33
  else:
34
  raise ValueError("Sorry, But please try a different combination of inputs.")
35
 
@@ -62,12 +92,6 @@ length = st.selectbox("Select Length:", ["short", "long"])
62
  language = st.selectbox("Select Language:", ["English", "Hindi"])
63
  mood = st.selectbox("Select Mood:", ["emotional", "sad", "happy", "horror", "comedy", "romantic"])
64
 
65
- image = None
66
-
67
- if uploaded_file is not None:
68
- image = Image.open(uploaded_file)
69
- st.image(image, caption="Uploaded Image", use_column_width=True)
70
-
71
  submit = st.button("Generate")
72
 
73
  # Handle submit action
@@ -75,7 +99,12 @@ if submit:
75
  if uploaded_file or input_prompt:
76
  try:
77
  image_data = input_image_setup(uploaded_file)
78
- response = get_gemini_response(input_prompt, image_data, genre, length, language, mood)
 
 
 
 
 
79
  st.subheader("Generated Content:")
80
  st.write(response)
81
  except ValueError as e:
 
3
  import os
4
  import google.generativeai as genai
5
  from PIL import Image
6
+ import textwrap
7
 
8
  load_dotenv()
9
 
10
+ # Configurar la API de Google Gemini
11
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
 
13
+ def upload_to_gemini(path, mime_type=None):
14
+ """Sube el archivo a Google Gemini."""
15
+ file = genai.upload_file(path, mime_type=mime_type)
16
+ return file
17
+
18
+ def get_gemini_response(input_prompt, image_data, genre, length, language, mood, model_name, temperature, top_p, top_k, max_output_tokens):
19
  if image_data:
20
+ uploaded_file = upload_to_gemini(image_data[0], mime_type=image_data[0]["mime_type"])
21
  full_prompt = f"""
22
  You are a famous creative writer. Look at the image provided and create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the image and contain realistic and emotional elements.
23
  """
 
30
  Make sure it contains realistic and emotional elements.
31
  """
32
 
33
+ # Configurar el modelo seleccionado
34
+ generation_config = {
35
+ "temperature": temperature,
36
+ "top_p": top_p,
37
+ "top_k": top_k,
38
+ "max_output_tokens": max_output_tokens,
39
+ "response_mime_type": "text/plain",
40
+ }
41
+
42
+ model = genai.GenerativeModel(
43
+ model_name=model_name,
44
+ generation_config=generation_config,
45
+ )
46
+
47
+ # Crear una sesi贸n de chat para interactuar con Gemini
48
+ chat_session = model.start_chat(
49
+ history=[
50
+ {
51
+ "role": "user",
52
+ "parts": [uploaded_file, full_prompt],
53
+ },
54
+ ]
55
+ )
56
+
57
+ # Enviar el mensaje y obtener la respuesta
58
+ response = chat_session.send_message("Generate content based on the prompt")
59
 
60
  # Check if response is valid and return text
61
+ if response and response.text:
62
+ return response.text
63
  else:
64
  raise ValueError("Sorry, But please try a different combination of inputs.")
65
 
 
92
  language = st.selectbox("Select Language:", ["English", "Hindi"])
93
  mood = st.selectbox("Select Mood:", ["emotional", "sad", "happy", "horror", "comedy", "romantic"])
94
 
 
 
 
 
 
 
95
  submit = st.button("Generate")
96
 
97
  # Handle submit action
 
99
  if uploaded_file or input_prompt:
100
  try:
101
  image_data = input_image_setup(uploaded_file)
102
+ response = get_gemini_response(input_prompt, image_data, genre, length, language, mood,
103
+ model_name="gemini-1.5-pro", # O el modelo que selecciones
104
+ temperature=0.9, # Ajusta estos valores seg煤n tu interfaz
105
+ top_p=0.95,
106
+ top_k=64,
107
+ max_output_tokens=1024)
108
  st.subheader("Generated Content:")
109
  st.write(response)
110
  except ValueError as e: