Spaces:
Runtime error
Runtime error
gamingflexer
commited on
Commit
·
5289cf3
1
Parent(s):
f587433
Django files
Browse files- .gitignore +1 -2
- src/app/api/models.py +20 -2
- src/app/api/serializers.py +7 -0
- src/app/api/urls.py +8 -1
- src/app/api/views.py +159 -6
- src/app/config.py +3 -0
- src/config.py +9 -0
.gitignore
CHANGED
|
@@ -159,5 +159,4 @@ cython_debug/
|
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
| 161 |
src/module/data/*
|
| 162 |
-
data/*
|
| 163 |
-
app
|
|
|
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
| 161 |
src/module/data/*
|
| 162 |
+
data/*
|
|
|
src/app/api/models.py
CHANGED
|
@@ -1,4 +1,22 @@
|
|
| 1 |
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class Product(models.Model):
|
| 4 |
barcode = models.CharField(max_length=20)
|
|
@@ -18,7 +36,7 @@ class Product(models.Model):
|
|
| 18 |
quantity = models.IntegerField(null=True, blank=True)
|
| 19 |
mrp = models.CharField(max_length=100, blank=True, null=True)
|
| 20 |
|
| 21 |
-
def
|
| 22 |
return self.product_name
|
| 23 |
|
| 24 |
|
|
@@ -40,4 +58,4 @@ class Database(models.Model):
|
|
| 40 |
quantity = models.IntegerField(null=True, blank=True)
|
| 41 |
promotion_on_the_pack = models.CharField(max_length=100, blank=True, null=True)
|
| 42 |
type_of_packaging = models.CharField(max_length=100, blank=True, null=True)
|
| 43 |
-
mrp = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
| 1 |
from django.db import models
|
| 2 |
+
import uuid
|
| 3 |
+
from django.urls.base import reverse
|
| 4 |
+
|
| 5 |
+
class Record(models.Model):
|
| 6 |
+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
| 7 |
+
voice_record = models.FileField(upload_to="records")
|
| 8 |
+
language = models.CharField(max_length=50, null=True, blank=True)
|
| 9 |
+
|
| 10 |
+
class Meta:
|
| 11 |
+
verbose_name = "Record"
|
| 12 |
+
verbose_name_plural = "Records"
|
| 13 |
+
|
| 14 |
+
def __str__(self):
|
| 15 |
+
return str(self.id)
|
| 16 |
+
|
| 17 |
+
def get_absolute_url(self):
|
| 18 |
+
return reverse("record_detail", kwargs={"id": str(self.id)})
|
| 19 |
+
|
| 20 |
|
| 21 |
class Product(models.Model):
|
| 22 |
barcode = models.CharField(max_length=20)
|
|
|
|
| 36 |
quantity = models.IntegerField(null=True, blank=True)
|
| 37 |
mrp = models.CharField(max_length=100, blank=True, null=True)
|
| 38 |
|
| 39 |
+
def __str__(self):
|
| 40 |
return self.product_name
|
| 41 |
|
| 42 |
|
|
|
|
| 58 |
quantity = models.IntegerField(null=True, blank=True)
|
| 59 |
promotion_on_the_pack = models.CharField(max_length=100, blank=True, null=True)
|
| 60 |
type_of_packaging = models.CharField(max_length=100, blank=True, null=True)
|
| 61 |
+
mrp = models.CharField(max_length=100, blank=True, null=True)
|
src/app/api/serializers.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from rest_framework import serializers
|
| 2 |
+
from .models import Product
|
| 3 |
+
|
| 4 |
+
class ProductSerializer(serializers.ModelSerializer):
|
| 5 |
+
class Meta:
|
| 6 |
+
model = Product
|
| 7 |
+
fields = '__all__'
|
src/app/api/urls.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
| 1 |
from django.urls import path
|
| 2 |
from . import views
|
|
|
|
| 3 |
|
| 4 |
urlpatterns = [
|
| 5 |
path('', views.catalouge_page, name='index'),
|
| 6 |
-
path('product', views.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
]
|
|
|
|
| 1 |
from django.urls import path
|
| 2 |
from . import views
|
| 3 |
+
from .views import ProductAPIView
|
| 4 |
|
| 5 |
urlpatterns = [
|
| 6 |
path('', views.catalouge_page, name='index'),
|
| 7 |
+
path('product/<int:product_id>/', views.product_page, name='product_detail'),
|
| 8 |
+
path('product-voice/<int:product_id>/', views.edit_voice_product, name='voice_product_detail'),
|
| 9 |
+
path('product/<int:product_id>/edit/', views.edit_product, name='edit_product'),
|
| 10 |
+
path('upload/', views.upload_image_and_audio, name='add_product_by_image'),
|
| 11 |
+
path('api/products/', ProductAPIView.as_view(), name='product_api'),
|
| 12 |
+
path("record/", views.record, name="record"),
|
| 13 |
+
path("record/detail/<uuid:id>/", views.record_detail, name="record_detail"),
|
| 14 |
]
|
src/app/api/views.py
CHANGED
|
@@ -1,10 +1,163 @@
|
|
| 1 |
-
from django.http import HttpResponse
|
| 2 |
from django.template import loader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def catalouge_page(request):
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from django.http import HttpResponse, HttpResponseRedirect
|
| 2 |
from django.template import loader
|
| 3 |
+
from django.conf import settings
|
| 4 |
+
from django.urls import reverse
|
| 5 |
+
from django.shortcuts import render, get_object_or_404
|
| 6 |
+
from rest_framework.views import APIView
|
| 7 |
+
from rest_framework.response import Response
|
| 8 |
+
from rest_framework import status
|
| 9 |
+
from .models import Product,Record
|
| 10 |
+
from django.contrib import messages
|
| 11 |
+
from .serializers import ProductSerializer
|
| 12 |
+
from django.http import JsonResponse
|
| 13 |
+
import os
|
| 14 |
+
from config import BASE_PATH
|
| 15 |
|
| 16 |
def catalouge_page(request):
|
| 17 |
+
# Fetch all products from the database
|
| 18 |
+
products = Product.objects.all()
|
| 19 |
+
for product in products:
|
| 20 |
+
if product.images_paths:
|
| 21 |
+
# Split the images_paths string and get the URL of the first image
|
| 22 |
+
image_urls = product.images_paths.split(',')
|
| 23 |
+
product.first_image_url = image_urls[0] if image_urls else None
|
| 24 |
+
else:
|
| 25 |
+
product.first_image_url = None
|
| 26 |
+
context = {
|
| 27 |
+
'products': products
|
| 28 |
+
}
|
| 29 |
+
return render(request, 'catalouge.html', context)
|
| 30 |
|
| 31 |
+
|
| 32 |
+
def product_page(request, product_id):
|
| 33 |
+
product = Product.objects.get(pk=product_id)
|
| 34 |
+
if product.images_paths:
|
| 35 |
+
product.images_paths = product.images_paths.split(",")
|
| 36 |
+
return render(request, 'product_page.html', {'product': product})
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def edit_product(request, product_id):
|
| 40 |
+
product = get_object_or_404(Product, pk=product_id)
|
| 41 |
+
|
| 42 |
+
if request.method == 'POST':
|
| 43 |
+
# Update the product fields with the submitted data
|
| 44 |
+
product.barcode = request.POST.get('barcode')
|
| 45 |
+
product.brand = request.POST.get('brand')
|
| 46 |
+
product.sub_brand = request.POST.get('sub_brand')
|
| 47 |
+
product.manufacturer = request.POST.get('manufacturer')
|
| 48 |
+
# Update other fields similarly
|
| 49 |
+
|
| 50 |
+
# Save the updated product
|
| 51 |
+
product.save()
|
| 52 |
+
|
| 53 |
+
# Redirect to the product page after saving
|
| 54 |
+
return HttpResponseRedirect(reverse('product_detail', args=(product.id,)))
|
| 55 |
+
|
| 56 |
+
return render(request, 'edit_product.html', {'product': product})
|
| 57 |
+
|
| 58 |
+
def catalouge_page(request):
|
| 59 |
+
# Fetch all products from the database
|
| 60 |
+
products = Product.objects.all()
|
| 61 |
+
|
| 62 |
+
# Handle search functionality
|
| 63 |
+
query = request.GET.get('q')
|
| 64 |
+
if query:
|
| 65 |
+
products = products.filter(product_name__icontains=query)
|
| 66 |
+
|
| 67 |
+
for product in products:
|
| 68 |
+
if product.images_paths:
|
| 69 |
+
# Split the images_paths string and get the URL of the first image
|
| 70 |
+
image_urls = product.images_paths.split(',')
|
| 71 |
+
product.first_image_url = image_urls[0] if image_urls else None
|
| 72 |
+
else:
|
| 73 |
+
product.first_image_url = None
|
| 74 |
+
|
| 75 |
+
context = {
|
| 76 |
+
'products': products,
|
| 77 |
+
'search_query': query # Pass the search query to display in the search bar
|
| 78 |
+
}
|
| 79 |
+
return render(request, 'catalouge.html', context)
|
| 80 |
+
|
| 81 |
+
def upload_image_and_audio(request):
|
| 82 |
+
if request.method == 'POST' and request.FILES.getlist('images'):
|
| 83 |
+
images = request.FILES.getlist('images')
|
| 84 |
+
for image in images:
|
| 85 |
+
# Save the image locally in the static directory
|
| 86 |
+
image_name = image.name
|
| 87 |
+
image_path = os.path.join(BASE_PATH,'app','main',settings.STATIC_ROOT, 'images', image_name)
|
| 88 |
+
with open(image_path, 'wb') as f:
|
| 89 |
+
for chunk in image.chunks():
|
| 90 |
+
f.write(chunk)
|
| 91 |
+
# Process the uploaded image and get product details
|
| 92 |
+
# product_detail = get_data_openai(image_path)
|
| 93 |
+
# product_details.append(product_detail)
|
| 94 |
+
|
| 95 |
+
# Handle audio recording
|
| 96 |
+
if 'audio_data' in request.POST:
|
| 97 |
+
# Save the audio data locally
|
| 98 |
+
audio_data = request.POST['audio_data']
|
| 99 |
+
audio_path = os.path.join(settings.STATIC_ROOT, 'audio', 'recorded_audio.wav')
|
| 100 |
+
with open(audio_path, 'wb') as f:
|
| 101 |
+
f.write(audio_data)
|
| 102 |
+
# Process the uploaded image and get product details
|
| 103 |
+
# product_detail = get_data_openai(image_path)
|
| 104 |
+
# product_details.append(product_detail)
|
| 105 |
+
# Dummy data for testing - You can replace this with actual data retrieved from the AI model
|
| 106 |
+
product_details = []
|
| 107 |
+
dummy_data = [
|
| 108 |
+
{'barcode': '123456', 'brand': 'Brand A', 'manufactured_by': 'Manufacturer A', 'product_name': 'Product A', 'weight': 1.5, 'variant': 'Variant A', 'net_content': '100ml', 'price': 10.0, 'parent_category': 'Category A', 'child_category': 'Subcategory A', 'description': 'Description for Product A', 'quantity': 100, 'mrp': '20.0'},
|
| 109 |
+
{'barcode': '654321', 'brand': 'Brand B', 'manufactured_by': 'Manufacturer B', 'product_name': 'Product B', 'weight': 2.0, 'variant': 'Variant B', 'net_content': '200ml', 'price': 15.0, 'parent_category': 'Category B', 'child_category': 'Subcategory B', 'description': 'Description for Product B', 'quantity': 200, 'mrp': '25.0'}
|
| 110 |
+
]
|
| 111 |
+
product_details.extend(dummy_data)
|
| 112 |
+
return JsonResponse(dummy_data[0], safe=False) # Return product details as JSON response
|
| 113 |
+
return render(request, 'upload_image.html')
|
| 114 |
+
|
| 115 |
+
class ProductAPIView(APIView):
|
| 116 |
+
def post(self, request, format=None):
|
| 117 |
+
serializer = ProductSerializer(data=request.data)
|
| 118 |
+
if serializer.is_valid():
|
| 119 |
+
serializer.save()
|
| 120 |
+
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
| 121 |
+
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
| 122 |
+
|
| 123 |
+
def edit_voice_product(request,product_id):
|
| 124 |
+
if request.method == 'POST':
|
| 125 |
+
# Handle voice file submission here
|
| 126 |
+
voice_file = request.FILES.get('voice_file')
|
| 127 |
+
if voice_file:
|
| 128 |
+
# Save the voice file to a specific location or process it as needed
|
| 129 |
+
# For example, you can save it to the media directory
|
| 130 |
+
# Assuming you have a media directory configured in your Django settings
|
| 131 |
+
with open('media/voice_files/' + voice_file.name, 'wb') as f:
|
| 132 |
+
for chunk in voice_file.chunks():
|
| 133 |
+
f.write(chunk)
|
| 134 |
+
return JsonResponse({'message': 'Voice file submitted successfully.'})
|
| 135 |
+
else:
|
| 136 |
+
return JsonResponse({'error': 'No voice file submitted.'}, status=400)
|
| 137 |
+
else:
|
| 138 |
+
return render(request, 'edit_voice_product.html')
|
| 139 |
+
|
| 140 |
+
def record(request):
|
| 141 |
+
if request.method == "POST":
|
| 142 |
+
audio_file = request.FILES.get("recorded_audio")
|
| 143 |
+
language = request.POST.get("language")
|
| 144 |
+
record = Record.objects.create(language=language, voice_record=audio_file)
|
| 145 |
+
record.save()
|
| 146 |
+
messages.success(request, "Audio recording successfully added!")
|
| 147 |
+
return JsonResponse(
|
| 148 |
+
{
|
| 149 |
+
"url": record.get_absolute_url(),
|
| 150 |
+
"success": True,
|
| 151 |
+
}
|
| 152 |
+
)
|
| 153 |
+
context = {"page_title": "Record audio"}
|
| 154 |
+
return render(request, "record.html", context)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def record_detail(request, id):
|
| 158 |
+
record = get_object_or_404(Record, id=id)
|
| 159 |
+
context = {
|
| 160 |
+
"page_title": "Recorded audio detail",
|
| 161 |
+
"record": record,
|
| 162 |
+
}
|
| 163 |
+
return render(request, "record_detail.html", context)
|
src/app/config.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
src/config.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from decouple import config
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
OPENAI_API_KEY = config('OPENAI_API_KEY', default="")
|
| 5 |
+
SQL_URL = config('SQL_URL', default="sqlite:///./db.sqlite3")
|
| 6 |
+
emmbedding_model = "text-embedding-3-large"
|
| 7 |
+
|
| 8 |
+
key = "f375d6130d4d4c518d69fe4716b5a6de"
|
| 9 |
+
endpoint = "https://bintix-ocr.cognitiveservices.azure.com/"
|