Spaces:
Sleeping
Sleeping
gamingflexer
commited on
Commit
•
eeea261
1
Parent(s):
5329974
Add audio processing functionality and update product details
Browse files- .gitignore +1 -0
- src/app2.py +45 -9
- src/app_utils.py +2 -2
.gitignore
CHANGED
@@ -174,3 +174,4 @@ src/app/api/migrations/0006_alter_product_barcode_alter_product_brand_and_more.p
|
|
174 |
src/app/api/migrations/0007_alter_product_price.py
|
175 |
src/app/media/images/*
|
176 |
src/app/api/module/image.ipynb
|
|
|
|
174 |
src/app/api/migrations/0007_alter_product_price.py
|
175 |
src/app/media/images/*
|
176 |
src/app/api/module/image.ipynb
|
177 |
+
src/audio/*.wav
|
src/app2.py
CHANGED
@@ -2,9 +2,16 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
import os
|
4 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
7 |
-
SEVER_IP = os.environ.get("SEVER_IP")
|
8 |
|
9 |
def get_total_number_of_products():
|
10 |
response = requests.get(f'{SEVER_IP}/api/total_number_of_products/')
|
@@ -20,21 +27,52 @@ def search_products(product_name):
|
|
20 |
else:
|
21 |
return pd.DataFrame([]) # Return an empty DataFrame in case of an error
|
22 |
|
23 |
-
def
|
24 |
-
response = requests.
|
25 |
if response.status_code == 200:
|
26 |
return response.json() # Returns the product details as a dictionary
|
27 |
else:
|
28 |
return {"error": f"Product with ID {product_id} not found or error occurred."}
|
29 |
|
30 |
-
def sample_fun(voice_input,
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)) as demo:
|
35 |
|
36 |
with gr.Tab("Add Your Image"):
|
37 |
-
voice_input = gr.Audio(
|
38 |
prodcut_id = gr.Textbox(label="Enter Product ID")
|
39 |
with gr.Row():
|
40 |
submit_button_tab_1 = gr.Button("Start")
|
@@ -50,8 +88,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, seconda
|
|
50 |
'child_category', 'sub_child_category', 'images_paths', 'description',
|
51 |
'quantity', 'promotion_on_the_pack', 'type_of_packaging', 'mrp'])
|
52 |
|
53 |
-
|
54 |
-
|
55 |
submit_button_tab_1.click(fn=sample_fun,inputs=[voice_input,prodcut_id])
|
56 |
submit_button_tab_4.click(fn=search_products,inputs=[embbed_text_search] ,outputs= dataframe_output_tab_4)
|
57 |
|
|
|
2 |
import pandas as pd
|
3 |
import os
|
4 |
import requests
|
5 |
+
from audio_text import whisper_openai
|
6 |
+
from app_utils import voice_edit, extract_json_from_text, getname
|
7 |
+
import uuid
|
8 |
+
import soundfile as sf
|
9 |
+
import time
|
10 |
+
|
11 |
+
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
|
12 |
|
13 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
14 |
+
SEVER_IP = os.environ.get("SEVER_IP","http://34.122.223.224:9002/")
|
15 |
|
16 |
def get_total_number_of_products():
|
17 |
response = requests.get(f'{SEVER_IP}/api/total_number_of_products/')
|
|
|
27 |
else:
|
28 |
return pd.DataFrame([]) # Return an empty DataFrame in case of an error
|
29 |
|
30 |
+
def update_product_details_by_id(product_id,payload):
|
31 |
+
response = requests.put(f'{SEVER_IP}/api/update_product/{product_id}/',data=payload)
|
32 |
if response.status_code == 200:
|
33 |
return response.json() # Returns the product details as a dictionary
|
34 |
else:
|
35 |
return {"error": f"Product with ID {product_id} not found or error occurred."}
|
36 |
|
37 |
+
def sample_fun(voice_input, product_id, progress=gr.Progress()):
|
38 |
+
|
39 |
+
audio_path = str(uuid.uuid4().hex) + ".wav"
|
40 |
+
print(voice_input)
|
41 |
+
sample_rate,audio_data = voice_input
|
42 |
+
progress(0.1, desc="Collecting audio data")
|
43 |
+
# audio_data = audio_data.reshape(-1, 1)
|
44 |
+
audio_save_path = os.path.join(BASE_PATH,"audio",audio_path)
|
45 |
+
sf.write(audio_save_path, audio_data, sample_rate)
|
46 |
+
# print("Product ID:", product_id)
|
47 |
+
time.sleep(2)
|
48 |
+
transcription = whisper_openai(audio_save_path)
|
49 |
+
# print("Transcription:", transcription)
|
50 |
+
prompt = voice_edit.format(text = transcription)
|
51 |
+
# print("Prompt:", prompt)
|
52 |
+
name = getname(prompt)
|
53 |
+
try:
|
54 |
+
json_data = extract_json_from_text(name)
|
55 |
+
except Exception as e:
|
56 |
+
print(f"-->Exception occurred while extracting JSON: {str(e)}")
|
57 |
+
# json_data['product_id'] = product_id
|
58 |
+
json_data_to_add = {}
|
59 |
+
progress(0.4, desc="Collecting Links")
|
60 |
+
for key in json_data:
|
61 |
+
if json_data[key] == "null" or json_data[key] == "" or json_data[key] == None:
|
62 |
+
pass
|
63 |
+
else:
|
64 |
+
json_data_to_add[key] = json_data[key]
|
65 |
+
print(json_data_to_add)
|
66 |
+
progress(0.7, desc="Collecting Links")
|
67 |
+
update_product_details_by_id(product_id,json_data)
|
68 |
+
progress(0.9, desc="Collecting Links")
|
69 |
+
return json_data
|
70 |
+
|
71 |
|
72 |
with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)) as demo:
|
73 |
|
74 |
with gr.Tab("Add Your Image"):
|
75 |
+
voice_input = gr.Audio(sources=["microphone"])
|
76 |
prodcut_id = gr.Textbox(label="Enter Product ID")
|
77 |
with gr.Row():
|
78 |
submit_button_tab_1 = gr.Button("Start")
|
|
|
88 |
'child_category', 'sub_child_category', 'images_paths', 'description',
|
89 |
'quantity', 'promotion_on_the_pack', 'type_of_packaging', 'mrp'])
|
90 |
|
|
|
|
|
91 |
submit_button_tab_1.click(fn=sample_fun,inputs=[voice_input,prodcut_id])
|
92 |
submit_button_tab_4.click(fn=search_products,inputs=[embbed_text_search] ,outputs= dataframe_output_tab_4)
|
93 |
|
src/app_utils.py
CHANGED
@@ -23,7 +23,7 @@ voice_edit = dedent("""
|
|
23 |
"unit": per pack
|
24 |
"Quantity": 1, ##num of products visible
|
25 |
"parent_category": from the above given list"
|
26 |
-
"ingredients":
|
27 |
"calorie_count": 12 ##calorie count
|
28 |
"marketed_by": ##sample_marketer
|
29 |
"manufactured_by": ##manufacturer
|
@@ -54,7 +54,7 @@ def getname(prompt):
|
|
54 |
|
55 |
|
56 |
def extract_json_from_text(text):
|
57 |
-
text = str(text)
|
58 |
print(f"Extracting JSON from text: {text}")
|
59 |
try:
|
60 |
# Find the JSON part within the text
|
|
|
23 |
"unit": per pack
|
24 |
"Quantity": 1, ##num of products visible
|
25 |
"parent_category": from the above given list"
|
26 |
+
"ingredients": "ingredient1", "ingredient2", "ingredient3" ##list of ingredients comma separated
|
27 |
"calorie_count": 12 ##calorie count
|
28 |
"marketed_by": ##sample_marketer
|
29 |
"manufactured_by": ##manufacturer
|
|
|
54 |
|
55 |
|
56 |
def extract_json_from_text(text):
|
57 |
+
text = str(text).lower()
|
58 |
print(f"Extracting JSON from text: {text}")
|
59 |
try:
|
60 |
# Find the JSON part within the text
|