Amovane commited on
Commit
54cb205
·
1 Parent(s): 525bf1c

feat: text to image

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. app.ipynb +87 -0
  3. app.py +22 -0
  4. requirements.txt +84 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ venv
2
+ .env
app.ipynb ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "Running on local URL: http://127.0.0.1:7861\n",
13
+ "\n",
14
+ "To create a public link, set `share=True` in `launch()`.\n"
15
+ ]
16
+ },
17
+ {
18
+ "data": {
19
+ "text/html": [
20
+ "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
21
+ ],
22
+ "text/plain": [
23
+ "<IPython.core.display.HTML object>"
24
+ ]
25
+ },
26
+ "metadata": {},
27
+ "output_type": "display_data"
28
+ },
29
+ {
30
+ "data": {
31
+ "text/plain": []
32
+ },
33
+ "execution_count": 2,
34
+ "metadata": {},
35
+ "output_type": "execute_result"
36
+ }
37
+ ],
38
+ "source": [
39
+ "import os\n",
40
+ "import gradio as gr\n",
41
+ "import requests\n",
42
+ "%env OPENAI_KEY\n",
43
+ "openai_key = os.getenv(\"OPENAI_KEY\")\n",
44
+ "\n",
45
+ "def ganerate(prompt):\n",
46
+ " n = 1\n",
47
+ " size = \"512x512\"\n",
48
+ " data = {\"n\": n, \"prompt\": prompt, \"size\": size}\n",
49
+ " headers = {\"Content-Type\": \"application/json\",\n",
50
+ " \"Authorization\": f\"\"\"Bearer {openai_key}\"\"\"}\n",
51
+ " result = requests.post(\n",
52
+ " \"https://api.openai.com/v1/images/generations\", json=data, headers=headers)\n",
53
+ " return result.json()[\"data\"][0][\"url\"]\n",
54
+ "\n",
55
+ "iface = gr.Interface(\n",
56
+ " fn=ganerate,\n",
57
+ " inputs=[\n",
58
+ " gr.Textbox(placeholder=\"Prompt\", label=\"Prompt\"),\n",
59
+ " ],\n",
60
+ " outputs=\"image\")\n",
61
+ "iface.launch()\n"
62
+ ]
63
+ }
64
+ ],
65
+ "metadata": {
66
+ "kernelspec": {
67
+ "display_name": "venv",
68
+ "language": "python",
69
+ "name": "python3"
70
+ },
71
+ "language_info": {
72
+ "codemirror_mode": {
73
+ "name": "ipython",
74
+ "version": 3
75
+ },
76
+ "file_extension": ".py",
77
+ "mimetype": "text/x-python",
78
+ "name": "python",
79
+ "nbconvert_exporter": "python",
80
+ "pygments_lexer": "ipython3",
81
+ "version": "3.11.2"
82
+ },
83
+ "orig_nbformat": 4
84
+ },
85
+ "nbformat": 4,
86
+ "nbformat_minor": 2
87
+ }
app.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ openai_key = os.getenv("OPENAI_KEY")
5
+
6
+ def ganerate(prompt):
7
+ n = 1
8
+ size = "512x512"
9
+ data = {"n": n, "prompt": prompt, "size": size}
10
+ headers = {"Content-Type": "application/json",
11
+ "Authorization": f"""Bearer {openai_key}"""}
12
+ result = requests.post(
13
+ "https://api.openai.com/v1/images/generations", json=data, headers=headers)
14
+ return result.json()["data"][0]["url"]
15
+
16
+ iface = gr.Interface(
17
+ fn=ganerate,
18
+ inputs=[
19
+ gr.Textbox(placeholder="Prompt", label="Prompt"),
20
+ ],
21
+ outputs="image")
22
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ altair==5.1.1
3
+ annotated-types==0.5.0
4
+ anyio==3.7.1
5
+ appnope==0.1.3
6
+ asttokens==2.4.0
7
+ attrs==23.1.0
8
+ backcall==0.2.0
9
+ certifi==2023.7.22
10
+ charset-normalizer==3.2.0
11
+ click==8.1.7
12
+ comm==0.1.4
13
+ contourpy==1.1.0
14
+ cycler==0.11.0
15
+ debugpy==1.7.0
16
+ decorator==5.1.1
17
+ executing==1.2.0
18
+ fastapi==0.103.1
19
+ ffmpy==0.3.1
20
+ filelock==3.12.3
21
+ fonttools==4.42.1
22
+ fsspec==2023.9.0
23
+ gradio==3.43.2
24
+ gradio_client==0.5.0
25
+ h11==0.14.0
26
+ httpcore==0.18.0
27
+ httpx==0.25.0
28
+ huggingface-hub==0.17.1
29
+ idna==3.4
30
+ importlib-resources==6.0.1
31
+ ipykernel==6.25.2
32
+ ipython==8.15.0
33
+ jedi==0.19.0
34
+ Jinja2==3.1.2
35
+ jsonschema==4.19.0
36
+ jsonschema-specifications==2023.7.1
37
+ jupyter_client==8.3.1
38
+ jupyter_core==5.3.1
39
+ kiwisolver==1.4.5
40
+ MarkupSafe==2.1.3
41
+ matplotlib==3.7.3
42
+ matplotlib-inline==0.1.6
43
+ nest-asyncio==1.5.7
44
+ numpy==1.25.2
45
+ orjson==3.9.7
46
+ packaging==23.1
47
+ pandas==2.1.0
48
+ parso==0.8.3
49
+ pexpect==4.8.0
50
+ pickleshare==0.7.5
51
+ Pillow==10.0.0
52
+ platformdirs==3.10.0
53
+ prompt-toolkit==3.0.39
54
+ psutil==5.9.5
55
+ ptyprocess==0.7.0
56
+ pure-eval==0.2.2
57
+ pydantic==2.3.0
58
+ pydantic_core==2.6.3
59
+ pydub==0.25.1
60
+ Pygments==2.16.1
61
+ pyparsing==3.1.1
62
+ python-dateutil==2.8.2
63
+ python-multipart==0.0.6
64
+ pytz==2023.3.post1
65
+ PyYAML==6.0.1
66
+ pyzmq==25.1.1
67
+ referencing==0.30.2
68
+ requests==2.31.0
69
+ rpds-py==0.10.2
70
+ semantic-version==2.10.0
71
+ six==1.16.0
72
+ sniffio==1.3.0
73
+ stack-data==0.6.2
74
+ starlette==0.27.0
75
+ toolz==0.12.0
76
+ tornado==6.3.3
77
+ tqdm==4.66.1
78
+ traitlets==5.9.0
79
+ typing_extensions==4.7.1
80
+ tzdata==2023.3
81
+ urllib3==2.0.4
82
+ uvicorn==0.23.2
83
+ wcwidth==0.2.6
84
+ websockets==11.0.3