takamarou commited on
Commit
4690160
·
1 Parent(s): 6dfd733

moving to fastapi

Browse files
app.py CHANGED
@@ -1,30 +1,54 @@
 
1
  import os
2
- import sys
3
- from gunicorn.app.base import BaseApplication
4
- from django.core.wsgi import get_wsgi_application
 
 
 
 
5
 
6
- class HFStickersApplication(BaseApplication):
7
- def __init__(self, app, options=None):
8
- self.options = options or {}
9
- self.application = app
10
- super().__init__()
11
 
12
- def load_config(self):
13
- config = {key: value for key, value in self.options.items()
14
- if key in self.cfg.settings and value is not None}
15
- for key, value in config.items():
16
- self.cfg.set(key.lower(), value)
17
 
18
- def load(self):
19
- return self.application
 
 
 
 
20
 
21
- if __name__ == '__main__':
22
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hf-stickers.settings') # Replace with your Django project's settings module
23
- django_app = get_wsgi_application()
 
24
 
25
- options = {
26
- 'bind': '0.0.0.0:7860',
27
- 'workers': 1,
28
- }
 
 
 
 
 
 
 
29
 
30
- HFStickersApplication(django_app, options).run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
  import os
3
+ import torch
4
+ import gradio as gr
5
+ from fastapi import FastAPI
6
+ from huggingface_hub import login
7
+ from diffusers import StableDiffusion3Pipeline, DDPMScheduler
8
+ from dotenv import load_dotenv
9
+ import uvicorn
10
 
11
+ login(token=os.getenv("HF_TOKEN"))
 
 
 
 
12
 
13
+ app = FastAPI()
 
 
 
 
14
 
15
+ pipeline = StableDiffusion3Pipeline.from_pretrained(
16
+ "stabilityai/stable-diffusion-3-medium",
17
+ revision="refs/pr/26",
18
+ torch_dtype=torch.float16,
19
+ )
20
+ pipeline.to("cuda")
21
 
22
+ @app.get("/")
23
+ def index():
24
+ print('here')
25
+ return "Hello"
26
 
27
+ @spaces.GPU
28
+ def generate(prompt, negative_prompt, num_inference_steps, height, width, guidance_scale):
29
+ print('start generate', prompt, negative_prompt, num_inference_steps, height, width, guidance_scale)
30
+ return pipeline(
31
+ prompt=prompt,
32
+ negative_prompt=negative_prompt,
33
+ num_inference_steps=num_inference_steps,
34
+ height=height,
35
+ width=width,
36
+ guidance_scale=guidance_scale
37
+ ).images
38
 
39
+ io = gr.Interface(
40
+ fn=generate,
41
+ inputs=[
42
+ gr.Textbox(label="Prompt", lines=3),
43
+ gr.Textbox(label="Negative Prompt", lines=2),
44
+ gr.Slider(label="Inference Steps", value=20, minimum=1, maximum=30, step=1),
45
+ gr.Number(label="Height"),
46
+ gr.Number(label="Width"),
47
+ gr.Slider(label="Guidance Scale", value=7, minimum=1, maximum=15, step=1)
48
+ ],
49
+ outputs=gr.Gallery(),
50
+ )
51
+
52
+ app = gr.mount_gradio_app(app, io, path="/gradio")
53
+ if __name__ == "__main__":
54
+ uvicorn.run(app, host="0.0.0.0", port=7680)
db.sqlite3 DELETED
File without changes
hf-stickers/__init__.py DELETED
File without changes
hf-stickers/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (131 Bytes)
 
hf-stickers/__pycache__/settings.cpython-310.pyc DELETED
Binary file (2.16 kB)
 
hf-stickers/__pycache__/wsgi.cpython-310.pyc DELETED
Binary file (542 Bytes)
 
hf-stickers/asgi.py DELETED
@@ -1,16 +0,0 @@
1
- """
2
- ASGI config for hf-stickers project.
3
-
4
- It exposes the ASGI callable as a module-level variable named ``application``.
5
-
6
- For more information on this file, see
7
- https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
8
- """
9
-
10
- import os
11
-
12
- from django.core.asgi import get_asgi_application
13
-
14
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hf-stickers.settings')
15
-
16
- application = get_asgi_application()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
hf-stickers/settings.py DELETED
@@ -1,103 +0,0 @@
1
- """
2
- Django settings for hf-stickers project.
3
-
4
- Generated by 'django-admin startproject' using Django 5.0.6.
5
-
6
- For more information on this file, see
7
- https://docs.djangoproject.com/en/5.0/topics/settings/
8
-
9
- For the full list of settings and their values, see
10
- https://docs.djangoproject.com/en/5.0/ref/settings/
11
- """
12
- import os
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
18
-
19
- # SECURITY WARNING: don't run with debug turned on in production!
20
- DEBUG = True
21
-
22
- ALLOWED_HOSTS = ["*"]
23
- CSRF_COOKIE_SECURE = True
24
- CSRF_COOKIE_HTTPONLY = True
25
-
26
- SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'django-insecure-#7!6z1!')
27
-
28
- # Application definition
29
-
30
- INSTALLED_APPS = [
31
- 'django.contrib.admin',
32
- 'django.contrib.auth',
33
- 'django.contrib.contenttypes',
34
- 'django.contrib.sessions',
35
- 'django.contrib.messages',
36
- 'django.contrib.staticfiles'
37
- ]
38
-
39
- MIDDLEWARE = [
40
- 'django.middleware.security.SecurityMiddleware',
41
- 'django.contrib.sessions.middleware.SessionMiddleware',
42
- 'django.middleware.common.CommonMiddleware',
43
- 'django.middleware.csrf.CsrfViewMiddleware',
44
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
45
- 'django.contrib.messages.middleware.MessageMiddleware',
46
- 'django.middleware.clickjacking.XFrameOptionsMiddleware'
47
- ]
48
-
49
- ROOT_URLCONF = 'hf-stickers.urls'
50
-
51
- SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
52
- SESSION_COOKIE_HTTPONLY = True
53
-
54
- WSGI_APPLICATION = 'hf-stickers.wsgi.application'
55
-
56
- # Internationalization
57
- # https://docs.djangoproject.com/en/5.0/topics/i18n/
58
-
59
- LANGUAGE_CODE = 'en-us'
60
-
61
- TIME_ZONE = 'UTC'
62
-
63
- USE_I18N = True
64
-
65
- USE_TZ = True
66
-
67
- # Default primary key field type
68
- # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
69
-
70
- DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
71
-
72
- LOGGING = {
73
- 'version': 1,
74
- 'disable_existing_loggers': False,
75
- 'formatters': {
76
- 'verbose': {
77
- 'format': ('%(asctime)s [%(process)d] [%(levelname)s] ' +
78
- 'pathname=%(pathname)s lineno=%(lineno)s ' +
79
- 'funcname=%(funcName)s %(message)s'),
80
- 'datefmt': '%Y-%m-%d %H:%M:%S'
81
- },
82
- 'simple': {
83
- 'format': '%(levelname)s %(message)s'
84
- }
85
- },
86
- 'handlers': {
87
- 'null': {
88
- 'level': 'DEBUG',
89
- 'class': 'logging.NullHandler',
90
- },
91
- 'console': {
92
- 'level': 'DEBUG',
93
- 'class': 'logging.StreamHandler',
94
- 'formatter': 'verbose'
95
- }
96
- },
97
- 'loggers': {
98
- 'main': {
99
- 'handlers': ['console'],
100
- 'level': 'DEBUG',
101
- }
102
- }
103
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
hf-stickers/urls.py DELETED
@@ -1,6 +0,0 @@
1
- from django.contrib import admin
2
- from django.urls import include, path
3
-
4
- urlpatterns = [
5
- path("", include("webapp.urls"))
6
- ]
 
 
 
 
 
 
 
hf-stickers/wsgi.py DELETED
@@ -1,16 +0,0 @@
1
- """
2
- WSGI config for hf-stickers project.
3
-
4
- It exposes the WSGI callable as a module-level variable named ``application``.
5
-
6
- For more information on this file, see
7
- https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
8
- """
9
-
10
- import os
11
-
12
- from django.core.wsgi import get_wsgi_application
13
-
14
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hf-stickers.settings')
15
-
16
- application = get_wsgi_application()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
manage.py DELETED
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env python
2
- """Django's command-line utility for administrative tasks."""
3
- import os
4
- import sys
5
-
6
-
7
- def main():
8
- """Run administrative tasks."""
9
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hf-stickers.settings')
10
- try:
11
- from django.core.management import execute_from_command_line
12
- except ImportError as exc:
13
- raise ImportError(
14
- "Couldn't import Django. Are you sure it's installed and "
15
- "available on your PYTHONPATH environment variable? Did you "
16
- "forget to activate a virtual environment?"
17
- ) from exc
18
- execute_from_command_line(sys.argv)
19
-
20
-
21
- if __name__ == '__main__':
22
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
management/commands/runserver.py DELETED
@@ -1,3 +0,0 @@
1
- from django.core.management.commands.runserver import Command as runserver
2
- class Command(runserver):
3
- default_port = "7680"
 
 
 
 
requirements.txt CHANGED
@@ -2,9 +2,7 @@ huggingface_hub
2
  diffusers
3
  transformers
4
  accelerate
5
- python-dotenv
6
  numpy<2.0.0,>=1.20.0
7
- sentencepiece
8
  gradio
9
- django
10
- gunicorn
 
2
  diffusers
3
  transformers
4
  accelerate
 
5
  numpy<2.0.0,>=1.20.0
 
6
  gradio
7
+ fastapi
8
+ uvicorn
webapp/__init__.py DELETED
File without changes
webapp/__pycache__/__init__.cpython-310.pyc DELETED
Binary file (126 Bytes)
 
webapp/admin.py DELETED
@@ -1,3 +0,0 @@
1
- from django.contrib import admin
2
-
3
- # Register your models here.
 
 
 
 
webapp/apps.py DELETED
@@ -1,6 +0,0 @@
1
- from django.apps import AppConfig
2
-
3
-
4
- class WebappConfig(AppConfig):
5
- default_auto_field = 'django.db.models.BigAutoField'
6
- name = 'webapp'
 
 
 
 
 
 
 
webapp/migrations/__init__.py DELETED
File without changes
webapp/models.py DELETED
@@ -1,3 +0,0 @@
1
- from django.db import models
2
-
3
- # Create your models here.
 
 
 
 
webapp/tests.py DELETED
@@ -1,3 +0,0 @@
1
- from django.test import TestCase
2
-
3
- # Create your tests here.
 
 
 
 
webapp/urls.py DELETED
@@ -1,7 +0,0 @@
1
- from django.urls import path
2
-
3
- from . import views
4
-
5
- urlpatterns = [
6
- path("", views.index, name="index")
7
- ]
 
 
 
 
 
 
 
 
webapp/views.py DELETED
@@ -1,4 +0,0 @@
1
- from django.http import HttpResponse
2
-
3
- def index(request):
4
- return HttpResponse('Hello!')