renator commited on
Commit
4b98fcb
·
1 Parent(s): e0491f4

create table for text to speech and update the schemas for user creation as well

Browse files
accounts/serializers.py CHANGED
@@ -1,43 +1,20 @@
1
- from django.contrib.auth.models import User
2
  from rest_framework import serializers
3
- from django.contrib.auth import authenticate
4
 
5
  class UserSerializer(serializers.ModelSerializer):
6
  password = serializers.CharField(write_only=True)
7
 
8
  def create(self, validated_data):
 
9
  user = User.objects.create_user(
10
  username=validated_data['username'],
11
- password=validated_data['password']
 
 
 
12
  )
13
  return user
14
 
15
  class Meta:
16
  model = User
17
- fields = ('id', 'username', 'password')
18
-
19
-
20
- class LoginSerializer(serializers.Serializer):
21
- username = serializers.CharField(max_length=150)
22
- password = serializers.CharField(max_length=128, write_only=True)
23
-
24
- def validate(self, data):
25
- username = data.get('username')
26
- password = data.get('password')
27
- print(username,password)
28
-
29
- if username and password:
30
- user = authenticate(username=username, password=password)
31
- print("user",user)
32
-
33
- if user:
34
- if user.is_active:
35
- data['user'] = user
36
- else:
37
- raise serializers.ValidationError("User is not active.")
38
- else:
39
- raise serializers.ValidationError("Unable to log in with the provided credentials.")
40
- else:
41
- raise serializers.ValidationError("Must include 'username' and 'password'.")
42
-
43
- return data
 
 
1
  from rest_framework import serializers
2
+ from django.contrib.auth.models import User
3
 
4
  class UserSerializer(serializers.ModelSerializer):
5
  password = serializers.CharField(write_only=True)
6
 
7
  def create(self, validated_data):
8
+ # Create the user instance
9
  user = User.objects.create_user(
10
  username=validated_data['username'],
11
+ email=validated_data.get('email', ''), # Default to empty string if email is not provided
12
+ password=validated_data['password'],
13
+ first_name=validated_data.get('first_name', ''), # Default to empty string if first_name is not provided
14
+ last_name=validated_data.get('last_name', '') # Default to empty string if last_name is not provided
15
  )
16
  return user
17
 
18
  class Meta:
19
  model = User
20
+ fields = ('id', 'username', 'password', 'first_name', 'last_name', 'email')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
accounts/views.py CHANGED
@@ -1,4 +1,4 @@
1
- from .serializers import LoginSerializer, UserSerializer
2
  from rest_framework import status
3
  from rest_framework.response import Response
4
  from rest_framework.generics import CreateAPIView
 
1
+ from .serializers import UserSerializer
2
  from rest_framework import status
3
  from rest_framework.response import Response
4
  from rest_framework.generics import CreateAPIView
config/settings.py CHANGED
@@ -11,7 +11,10 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
11
  """
12
 
13
 
 
14
  from pathlib import Path
 
 
15
 
16
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
17
  BASE_DIR = Path(__file__).resolve().parent.parent
@@ -70,6 +73,30 @@ SWAGGER_SETTINGS = {
70
  }
71
  }
72
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
74
 
75
  MIDDLEWARE = [
@@ -151,6 +178,8 @@ USE_TZ = True
151
  # https://docs.djangoproject.com/en/5.0/howto/static-files/
152
 
153
  STATIC_URL = 'static/'
 
 
154
 
155
  # Default primary key field type
156
  # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
 
11
  """
12
 
13
 
14
+ import os
15
  from pathlib import Path
16
+ import logging
17
+
18
 
19
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
20
  BASE_DIR = Path(__file__).resolve().parent.parent
 
73
  }
74
  }
75
  }
76
+ LOGGING = {
77
+ 'version': 1,
78
+ 'disable_existing_loggers': False,
79
+ 'handlers': {
80
+ 'file': {
81
+ 'level': 'DEBUG', # Choose the desired logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
82
+ 'class': 'logging.FileHandler',
83
+ 'filename': 'project.log', # Path to the log file
84
+ },
85
+ },
86
+ 'loggers': {
87
+ 'django': {
88
+ 'handlers': ['file'],
89
+ 'level': 'DEBUG', # Set the level for Django-related logs
90
+ 'propagate': True,
91
+ },
92
+ 'myapp': {
93
+ 'handlers': ['file'],
94
+ 'level': 'DEBUG', # Set the level for your app's logs
95
+ 'propagate': True,
96
+ },
97
+ },
98
+ }
99
+
100
  X_FRAME_OPTIONS = 'ALLOW-FROM https://huggingface.co/'
101
 
102
  MIDDLEWARE = [
 
178
  # https://docs.djangoproject.com/en/5.0/howto/static-files/
179
 
180
  STATIC_URL = 'static/'
181
+ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
182
+ MEDIA_URL = '/media/'
183
 
184
  # Default primary key field type
185
  # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
config/urls.py CHANGED
@@ -1,8 +1,11 @@
1
  from django.contrib import admin
2
- from django.urls import path, re_path, include
3
  from rest_framework import permissions
4
  from drf_yasg.views import get_schema_view
5
  from drf_yasg import openapi
 
 
 
6
 
7
  schema_view = get_schema_view(
8
  openapi.Info(
@@ -28,3 +31,6 @@ urlpatterns = [
28
  path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
29
 
30
  ]
 
 
 
 
1
  from django.contrib import admin
2
+ from django.urls import path, include
3
  from rest_framework import permissions
4
  from drf_yasg.views import get_schema_view
5
  from drf_yasg import openapi
6
+ from django.conf import settings
7
+ from django.conf.urls.static import static
8
+
9
 
10
  schema_view = get_schema_view(
11
  openapi.Info(
 
31
  path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
32
 
33
  ]
34
+
35
+ if settings.DEBUG:
36
+ urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
db.sqlite3 CHANGED
Binary files a/db.sqlite3 and b/db.sqlite3 differ
 
project.log CHANGED
The diff for this file is too large to render. See raw diff
 
texttovoice/migrations/0001_initial.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Generated by Django 5.0.1 on 2024-01-25 15:38
2
+
3
+ import django.db.models.deletion
4
+ from django.conf import settings
5
+ from django.db import migrations, models
6
+
7
+
8
+ class Migration(migrations.Migration):
9
+
10
+ initial = True
11
+
12
+ dependencies = [
13
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14
+ ]
15
+
16
+ operations = [
17
+ migrations.CreateModel(
18
+ name='TextToSpeech',
19
+ fields=[
20
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21
+ ('created_date', models.DateTimeField(auto_now_add=True)),
22
+ ('text', models.CharField(default='In the quest for a sustainable future, renewable energy emerges as a beacon of hope', max_length=255)),
23
+ ('speaker_wav', models.FileField(upload_to='speaker_wav/')),
24
+ ('output_wav', models.FileField(upload_to='output_wav/')),
25
+ ('language', models.CharField(default='en', max_length=2)),
26
+ ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_created_by', to=settings.AUTH_USER_MODEL)),
27
+ ],
28
+ options={
29
+ 'ordering': ['-created_date'],
30
+ 'abstract': False,
31
+ },
32
+ ),
33
+ ]
texttovoice/models.py CHANGED
@@ -1,3 +1,28 @@
 
1
  from django.db import models
 
2
 
3
  # Create your models here.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
  from django.db import models
3
+ from django.contrib.auth.models import User
4
 
5
  # Create your models here.
6
+ class BaseModel(models.Model):
7
+ created_by = models.ForeignKey(
8
+ User, on_delete=models.CASCADE, related_name="%(class)s_created_by"
9
+ )
10
+ created_date = models.DateTimeField(auto_now_add=True)
11
+
12
+ class Meta:
13
+ abstract = True
14
+ ordering = ["-created_date"]
15
+ class TextToSpeech(BaseModel):
16
+ text = models.CharField(
17
+ max_length=255,
18
+ default="In the quest for a sustainable future, renewable energy emerges as a beacon of hope"
19
+ )
20
+ speaker_wav = models.FileField(upload_to='speaker_wav/')
21
+ output_wav = models.FileField(upload_to='output_wav/')
22
+ language = models.CharField(
23
+ max_length=2, # Adjust the max length based on your language code requirements
24
+ default="en"
25
+ )
26
+
27
+ def __str__(self):
28
+ return f"TextToSpeech ID: {self.id}"
texttovoice/views.py CHANGED
@@ -10,7 +10,7 @@ from rest_framework.generics import CreateAPIView
10
  from TTS.api import TTS
11
  from rest_framework.authentication import TokenAuthentication
12
  from rest_framework.permissions import IsAuthenticated
13
- # from texttovoice.models import TextToSpeech
14
  from .serializers import TextToSpeechSerializer
15
  from rest_framework.parsers import MultiPartParser
16
  from drf_yasg import openapi
@@ -57,9 +57,6 @@ class TextToSpeechCreateView(CreateAPIView):
57
 
58
  # Log the start time
59
  start_time = time.time()
60
- print("start", start_time)
61
- logger.info(f"start time: {start_time} ")
62
-
63
  # Save the uploaded speaker file to a temporary location
64
  speaker_file_path = os.path.join('/tmp', speaker_wav.name)
65
  with open(speaker_file_path, "wb") as destination:
@@ -90,18 +87,17 @@ class TextToSpeechCreateView(CreateAPIView):
90
 
91
  # Use the file_iterator to create a FileResponse
92
 
93
- # TextToSpeech.objects.create(
94
- # text=text,
95
- # speaker_wav=speaker_wav,
96
- # output_wav=output_filename,
97
- # language=language,
98
- # created_by=request.user # Assign the authenticated user here
99
- # )
100
- response = FileResponse(output_filename, as_attachment=True, content_type='audio/wav')
101
 
102
  # Log the processing time using the logger
103
  logger.info(f"start time: {start_time} , end time: {end_time} and Processing time: {processing_time} seconds")
104
- print(f"start time: {start_time} , end time: {end_time} and Processing time: {processing_time} seconds")
105
 
106
  return response
107
 
 
10
  from TTS.api import TTS
11
  from rest_framework.authentication import TokenAuthentication
12
  from rest_framework.permissions import IsAuthenticated
13
+ from texttovoice.models import TextToSpeech
14
  from .serializers import TextToSpeechSerializer
15
  from rest_framework.parsers import MultiPartParser
16
  from drf_yasg import openapi
 
57
 
58
  # Log the start time
59
  start_time = time.time()
 
 
 
60
  # Save the uploaded speaker file to a temporary location
61
  speaker_file_path = os.path.join('/tmp', speaker_wav.name)
62
  with open(speaker_file_path, "wb") as destination:
 
87
 
88
  # Use the file_iterator to create a FileResponse
89
 
90
+ TextToSpeech.objects.create(
91
+ text=text,
92
+ speaker_wav=speaker_wav,
93
+ output_wav=output_filename,
94
+ language=language,
95
+ created_by=request.user # Assign the authenticated user here
96
+ )
97
+ response = FileResponse(file_iterator(output_filename), as_attachment=True, content_type='audio/wav')
98
 
99
  # Log the processing time using the logger
100
  logger.info(f"start time: {start_time} , end time: {end_time} and Processing time: {processing_time} seconds")
 
101
 
102
  return response
103