fffiloni commited on
Commit
6da2f84
·
verified ·
1 Parent(s): 06d5a93
Files changed (1) hide show
  1. app.py +11 -33
app.py CHANGED
@@ -1,17 +1,17 @@
1
  import spaces
2
  import gradio as gr
 
 
 
3
  import re
4
  import os
5
  import json
6
  from typing import Union
7
  hf_token = os.environ.get('HF_TOKEN')
8
 
9
- from gradio_client import Client, handle_file
10
-
11
- #client = Client("fffiloni/moondream2", hf_token=hf_token)
12
-
13
  from transformers import AutoTokenizer, AutoModelForCausalLM
14
 
 
15
  cap_model = AutoModelForCausalLM.from_pretrained(
16
  "vikhyatk/moondream2",
17
  revision="2025-06-21",
@@ -21,7 +21,6 @@ cap_model = AutoModelForCausalLM.from_pretrained(
21
 
22
  @spaces.GPU
23
  def infer_cap(image):
24
-
25
 
26
  # Captioning
27
  #print("Short caption:")
@@ -35,16 +34,14 @@ def infer_cap(image):
35
  return result
36
 
37
 
38
- model_path = "meta-llama/Llama-2-13b-chat-hf"
39
 
40
- tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False, use_auth_token=hf_token)
41
- model = AutoModelForCausalLM.from_pretrained(model_path, use_auth_token=hf_token).half().cuda()
 
42
 
43
  # FLUX
44
 
45
- import numpy as np
46
- import random
47
- import torch
48
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
49
  from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
50
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
@@ -143,20 +140,17 @@ Always ensure that:
143
  – Each perfume feels unique and consistent.
144
  Here is the scene description to analyze:
145
  \n<</SYS>>\n\n{} [/INST]"""
146
-
147
-
148
  prompt = instruction.format(scene)
149
 
150
  generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096)
151
  output_text = tokenizer.decode(generate_ids[0], skip_special_tokens=True)
152
- #print(generate_ids)
153
- #print(output_text)
154
  pattern = r'\[INST\].*?\[/INST\]'
155
  cleaned_text = re.sub(pattern, '', output_text, flags=re.DOTALL)
156
  return cleaned_text
157
 
158
  def extract_notes(text, section_name):
159
- import re
160
 
161
  bullet_pattern = r'(\*|\d+[a-zA-Z]?\.|-|•|\+|[a-zA-Z]\.)'
162
 
@@ -302,22 +296,8 @@ def extract_field(data: Union[str, dict], field_name: str) -> str:
302
  return str(value).strip()
303
 
304
 
305
- def get_text_after_colon(input_text):
306
- # Find the first occurrence of ":"
307
- colon_index = input_text.find(":")
308
-
309
- # Check if ":" exists in the input_text
310
- if colon_index != -1:
311
- # Extract the text after the colon
312
- result_text = input_text[colon_index + 1:].strip()
313
- return result_text
314
- else:
315
- # Return the original text if ":" is not found
316
- return input_text
317
-
318
- import pandas as pd
319
-
320
  # Load your perfume database once
 
321
  df = pd.read_excel('perfume_database_cleaned.xlsx')
322
 
323
  def extract_notes_for_comparison(data: Union[str, dict]) -> list[str]:
@@ -401,10 +381,8 @@ def find_best_perfumes_from_json(data: Union[str, dict], top_n: int = 5, thresho
401
  def infer(image_input):
402
 
403
  gr.Info('Calling Moondream model for caption ...')
404
-
405
  yield None, None, None, None
406
  moondream_result = infer_cap(image_input)
407
- print(moondream_result)
408
 
409
  llama_q = moondream_result
410
 
 
1
  import spaces
2
  import gradio as gr
3
+ import numpy as np
4
+ import random
5
+ import torch
6
  import re
7
  import os
8
  import json
9
  from typing import Union
10
  hf_token = os.environ.get('HF_TOKEN')
11
 
 
 
 
 
12
  from transformers import AutoTokenizer, AutoModelForCausalLM
13
 
14
+ # Load Moondream2 model for captioning
15
  cap_model = AutoModelForCausalLM.from_pretrained(
16
  "vikhyatk/moondream2",
17
  revision="2025-06-21",
 
21
 
22
  @spaces.GPU
23
  def infer_cap(image):
 
24
 
25
  # Captioning
26
  #print("Short caption:")
 
34
  return result
35
 
36
 
37
+ # Load Llama LLM
38
 
39
+ llm_model_path = "meta-llama/Llama-2-13b-chat-hf"
40
+ tokenizer = AutoTokenizer.from_pretrained(llm_model_path, use_fast=False, use_auth_token=hf_token)
41
+ model = AutoModelForCausalLM.from_pretrained(llm_model_path, use_auth_token=hf_token).half().cuda()
42
 
43
  # FLUX
44
 
 
 
 
45
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
46
  from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
47
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
 
140
  – Each perfume feels unique and consistent.
141
  Here is the scene description to analyze:
142
  \n<</SYS>>\n\n{} [/INST]"""
143
+
 
144
  prompt = instruction.format(scene)
145
 
146
  generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096)
147
  output_text = tokenizer.decode(generate_ids[0], skip_special_tokens=True)
148
+
 
149
  pattern = r'\[INST\].*?\[/INST\]'
150
  cleaned_text = re.sub(pattern, '', output_text, flags=re.DOTALL)
151
  return cleaned_text
152
 
153
  def extract_notes(text, section_name):
 
154
 
155
  bullet_pattern = r'(\*|\d+[a-zA-Z]?\.|-|•|\+|[a-zA-Z]\.)'
156
 
 
296
  return str(value).strip()
297
 
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  # Load your perfume database once
300
+ import pandas as pd
301
  df = pd.read_excel('perfume_database_cleaned.xlsx')
302
 
303
  def extract_notes_for_comparison(data: Union[str, dict]) -> list[str]:
 
381
  def infer(image_input):
382
 
383
  gr.Info('Calling Moondream model for caption ...')
 
384
  yield None, None, None, None
385
  moondream_result = infer_cap(image_input)
 
386
 
387
  llama_q = moondream_result
388