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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -45
app.py CHANGED
@@ -3,24 +3,19 @@ import streamlit as st
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
  """
 
24
  else:
25
  full_prompt = f"""
26
  You are a creative writer. Create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the following prompt:
@@ -29,37 +24,11 @@ def get_gemini_response(input_prompt, image_data, genre, length, language, mood,
29
 
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,6 +61,12 @@ length = st.selectbox("Select Length:", ["short", "long"])
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,12 +74,7 @@ if submit:
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:
@@ -115,3 +85,4 @@ if submit:
115
  st.sidebar.title("About")
116
  st.sidebar.info("This app uses AI to create stories, shayari, sher, ghazal, poems, or quotes based on an uploaded image or a provided prompt. "
117
  "Upload an image or enter a prompt, and select the genre, length, language, and mood to generate your content.")
 
 
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
+ model = genai.GenerativeModel('gemini-1.5-flash')
 
 
 
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
  """
18
+ response = model.generate_content([full_prompt, image_data[0]])
19
  else:
20
  full_prompt = f"""
21
  You are a creative writer. Create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the following prompt:
 
24
 
25
  Make sure it contains realistic and emotional elements.
26
  """
27
+ response = model.generate_content([full_prompt])
28
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Check if response is valid and return text
30
+ if response and response.parts:
31
+ return response.parts[0].text
32
  else:
33
  raise ValueError("Sorry, But please try a different combination of inputs.")
34
 
 
61
  language = st.selectbox("Select Language:", ["English", "Hindi"])
62
  mood = st.selectbox("Select Mood:", ["emotional", "sad", "happy", "horror", "comedy", "romantic"])
63
 
64
+ image = None
65
+
66
+ if uploaded_file is not None:
67
+ image = Image.open(uploaded_file)
68
+ st.image(image, caption="Uploaded Image", use_column_width=True)
69
+
70
  submit = st.button("Generate")
71
 
72
  # Handle submit action
 
74
  if uploaded_file or input_prompt:
75
  try:
76
  image_data = input_image_setup(uploaded_file)
77
+ response = get_gemini_response(input_prompt, image_data, genre, length, language, mood)
 
 
 
 
 
78
  st.subheader("Generated Content:")
79
  st.write(response)
80
  except ValueError as e:
 
85
  st.sidebar.title("About")
86
  st.sidebar.info("This app uses AI to create stories, shayari, sher, ghazal, poems, or quotes based on an uploaded image or a provided prompt. "
87
  "Upload an image or enter a prompt, and select the genre, length, language, and mood to generate your content.")
88
+