nia2 commited on
Commit
33d0fc7
·
verified ·
1 Parent(s): f1008a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -28
app.py CHANGED
@@ -1,13 +1,15 @@
 
1
  import gradio as gr
2
  import google.generativeai as genai
3
  from yolov5 import YOLOv5
4
  from PIL import Image
 
 
 
5
 
6
-
7
- #Clé Api
8
  genai.configure(api_key="AIzaSyB_Hnl_77gd1I8xs6iuLqKIoGHgsZMXm1M")
9
 
10
-
11
  # Le modèle Gemini Pro
12
  gemini_model = genai.GenerativeModel('gemini-pro')
13
 
@@ -15,6 +17,21 @@ gemini_model = genai.GenerativeModel('gemini-pro')
15
  yolo_model_path = "yolov5s.pt"
16
  yolo_model = YOLOv5(yolo_model_path, device="cpu")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Détection d'ingrédients
20
  def detect_ingredients(image):
@@ -50,33 +67,83 @@ def generate_recipe(ingredients):
50
  response = gemini_model.generate_content(prompt)
51
  return response.text
52
 
53
- response = gemini_model.generatecontent(prompt)
54
- return response.text
55
-
 
 
 
 
 
 
56
 
57
  # Interface Gradio
58
  def process_image(image):
59
- image = Image.open(image)
60
- ingredients = detect_ingredients(image)
61
- recipe = generate_recipe(ingredients)
62
- return f"Ingrédients détectés : {', '.join(ingredients)}\n\nRecette générée :\n{recipe}"
63
-
64
 
65
  # Mise à jour de l'interface Gradio
66
- iface = gr.Interface(
67
- fn=process_image,
68
- inputs=gr.Image(type="filepath"),
69
- outputs="text",
70
- title="Générateur de Recettes par Ingrédients",
71
- description="Téléchargez une image d'ingrédients pour générer une recette.",
72
- )
73
-
74
- # Pour lancer l'application
75
- if __name__ == "__main__":
76
- iface.launch()
77
-
78
-
79
-
80
-
81
-
82
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from yolov5 import YOLOv5
5
  from PIL import Image
6
+ from bark import SAMPLE_RATE, generate_audio, preload_models
7
+ from bark.generation import SUPPORTED_LANGS
8
+ from share_btn import community_icon_html, loading_icon_html, share_js
9
 
10
+ # Clé API
 
11
  genai.configure(api_key="AIzaSyB_Hnl_77gd1I8xs6iuLqKIoGHgsZMXm1M")
12
 
 
13
  # Le modèle Gemini Pro
14
  gemini_model = genai.GenerativeModel('gemini-pro')
15
 
 
17
  yolo_model_path = "yolov5s.pt"
18
  yolo_model = YOLOv5(yolo_model_path, device="cpu")
19
 
20
+ # Précharger les modèles de bark
21
+ DEBUG_MODE = False
22
+ if not DEBUG_MODE:
23
+ _ = preload_models()
24
+
25
+ # Prompts disponibles pour bark
26
+ AVAILABLE_PROMPTS = ["Unconditional", "Announcer"]
27
+ PROMPT_LOOKUP = {}
28
+ for _, lang in SUPPORTED_LANGS:
29
+ for n in range(10):
30
+ label = f"Speaker {n} ({lang})"
31
+ AVAILABLE_PROMPTS.append(label)
32
+ PROMPT_LOOKUP[label] = f"{lang}_speaker_{n}"
33
+ PROMPT_LOOKUP["Unconditional"] = None
34
+ PROMPT_LOOKUP["Announcer"] = "announcer"
35
 
36
  # Détection d'ingrédients
37
  def detect_ingredients(image):
 
67
  response = gemini_model.generate_content(prompt)
68
  return response.text
69
 
70
+ # Fonction de génération de TTS
71
+ def gen_tts(text, history_prompt):
72
+ history_prompt = PROMPT_LOOKUP[history_prompt]
73
+ if DEBUG_MODE:
74
+ audio_arr = np.zeros(SAMPLE_RATE)
75
+ else:
76
+ audio_arr = generate_audio(text, history_prompt=history_prompt)
77
+ audio_arr = (audio_arr * 32767).astype(np.int16)
78
+ return (SAMPLE_RATE, audio_arr)
79
 
80
  # Interface Gradio
81
  def process_image(image):
82
+ image = Image.open(image)
83
+ ingredients = detect_ingredients(image)
84
+ recipe = generate_recipe(ingredients)
85
+ return f"Ingrédients détectés : {', '.join(ingredients)}\n\nRecette générée :\n{recipe}"
 
86
 
87
  # Mise à jour de l'interface Gradio
88
+ css = """
89
+ #share-btn-container {
90
+ display: flex;
91
+ padding-left: 0.5rem !important;
92
+ padding-right: 0.5rem !important;
93
+ background-color: #000000;
94
+ justify-content: center;
95
+ align-items: center;
96
+ border-radius: 9999px !important;
97
+ width: 13rem;
98
+ margin-top: 10px;
99
+ margin-left: auto;
100
+ flex: unset !important;
101
+ }
102
+ #share-btn {
103
+ all: initial;
104
+ color: #ffffff;
105
+ font-weight: 600;
106
+ cursor: pointer;
107
+ font-family: 'IBM Plex Sans', sans-serif;
108
+ margin-left: 0.5rem !important;
109
+ padding-top: 0.25rem !important;
110
+ padding-bottom: 0.25rem !important;
111
+ right:0;
112
+ }
113
+ #share-btn * {
114
+ all: unset !important;
115
+ }
116
+ #share-btn-container div:nth-child(-n+2){
117
+ width: auto !important;
118
+ min-height: 0px !important;
119
+ }
120
+ #share-btn-container .wrap {
121
+ display: none !important;
122
+ }
123
+ """
124
+
125
+ with gr.Blocks(css=css) as block:
126
+ gr.Markdown("# Générateur de Recettes par Ingrédients")
127
+ gr.Markdown("Téléchargez une image d'ingrédients pour générer une recette.")
128
+ with gr.Row():
129
+ with gr.Column():
130
+ input_image = gr.Image(type="filepath", label="Image d'ingrédients")
131
+ run_button = gr.Button(text="Générer Recette", type="button")
132
+ with gr.Column():
133
+ output_text = gr.Textbox(label="Recette générée", lines=10)
134
+ audio_out = gr.Audio(label="Audio de la recette", type="numpy")
135
+ with gr.Row(visible=False) as share_row:
136
+ with gr.Group(elem_id="share-btn-container"):
137
+ community_icon = gr.HTML(community_icon_html)
138
+ loading_icon = gr.HTML(loading_icon_html)
139
+ share_button = gr.Button("Share to community", elem_id="share-btn")
140
+ share_button.click(None, [], [], _js=share_js)
141
+ inputs = [input_image]
142
+ outputs = [output_text, audio_out]
143
+ gr.Examples(examples=[], fn=process_image, inputs=inputs, outputs=outputs, cache_examples=True)
144
+ run_button.click(fn=lambda: gr.update(visible=False), inputs=None, outputs=share_row, queue=False).then(
145
+ fn=process_image, inputs=inputs, outputs=outputs, queue=True).then(
146
+ fn=lambda: gr.update(visible=True), inputs=None, outputs=share_row, queue=False)
147
+
148
+ block.queue()
149
+ block.launch()