Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
ost-aiap-miniproject2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Patrick Scheidegger
ost-aiap-miniproject2
Commits
78c35104
Commit
78c35104
authored
2 years ago
by
Patrick Scheidegger
Browse files
Options
Downloads
Patches
Plain Diff
yey
parent
f7620b1a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
miniproject2.ipynb
+57
-1
57 additions, 1 deletion
miniproject2.ipynb
with
57 additions
and
1 deletion
miniproject2.ipynb
+
57
−
1
View file @
78c35104
...
@@ -63,12 +63,68 @@
...
@@ -63,12 +63,68 @@
" f.write(base64.b64decode(image[\"base64\"]))\n"
" f.write(base64.b64decode(image[\"base64\"]))\n"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpenAI / ChatGPT"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": []
"source": [
"!pip install openai"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"random_scenes = [\n",
" [\"A woman in a white dress standing in a field of sunflowers\",\n",
" \"A man in a suit walking down a busy city street\",\n",
" \"A couple embracing on a beach at sunset\",\n",
" \"A family of four having a picnic in a park\",\n",
" \"A group of friends laughing around a campfire\"],\n",
" \n",
" [\"A woman in a red dress dancing in a nightclub\",\n",
" \"A man in a suit walking through a crowded market\",\n",
" \"A couple walking hand in hand through a forest\",\n",
" \"A family of four playing in a playground\",\n",
" \"A group of friends enjoying a sunset boat ride\"],\n",
" \n",
"\n"
]
}
],
"source": [
"import os\n",
"import openai\n",
"\n",
"text = \"Generate 5 random scenes for some stock images as a csv\"\n",
"\n",
"# Load your API key from an environment variable or secret management service\n",
"openai.api_key = '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'\n",
"\n",
"#'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'\n",
"\n",
"response = openai.Completion.create(model=\"text-davinci-003\", prompt=text, temperature=0, max_tokens=150)\n",
"\n",
"answers = response.choices[0].text\n",
"\n",
"print(response);\n",
"print(response.choices[0].text);"
]
}
}
],
],
"metadata": {
"metadata": {
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# AI Miniproject 2
# AI Miniproject 2
Bla bla
Bla bla
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# stability.ai & Stable diffusion
# stability.ai & Stable diffusion
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
base64
import
base64
import
os
import
os
import
requests
import
requests
engine_id
=
"
stable-diffusion-512-v2-1
"
engine_id
=
"
stable-diffusion-512-v2-1
"
api_host
=
'
https://api.stability.ai
'
api_host
=
'
https://api.stability.ai
'
api_key
=
'
sk-v1z1pueYOHD2Bu83YL9undyzC54WW89eJkCAI79e4zLLeje1
'
api_key
=
'
sk-v1z1pueYOHD2Bu83YL9undyzC54WW89eJkCAI79e4zLLeje1
'
response
=
requests
.
post
(
response
=
requests
.
post
(
f
"
{
api_host
}
/v1/generation/
{
engine_id
}
/text-to-image
"
,
f
"
{
api_host
}
/v1/generation/
{
engine_id
}
/text-to-image
"
,
headers
=
{
headers
=
{
"
Content-Type
"
:
"
application/json
"
,
"
Content-Type
"
:
"
application/json
"
,
"
Accept
"
:
"
application/json
"
,
"
Accept
"
:
"
application/json
"
,
"
Authorization
"
:
f
"
Bearer
{
api_key
}
"
"
Authorization
"
:
f
"
Bearer
{
api_key
}
"
},
},
json
=
{
json
=
{
"
text_prompts
"
:
[
"
text_prompts
"
:
[
{
{
"
text
"
:
"
A lighthouse on a cliff
"
"
text
"
:
"
A lighthouse on a cliff
"
}
}
],
],
"
cfg_scale
"
:
7
,
"
cfg_scale
"
:
7
,
"
clip_guidance_preset
"
:
"
FAST_BLUE
"
,
"
clip_guidance_preset
"
:
"
FAST_BLUE
"
,
"
height
"
:
512
,
"
height
"
:
512
,
"
width
"
:
512
,
"
width
"
:
512
,
"
samples
"
:
1
,
"
samples
"
:
1
,
"
steps
"
:
30
,
"
steps
"
:
30
,
},
},
)
)
if
response
.
status_code
!=
200
:
if
response
.
status_code
!=
200
:
raise
Exception
(
"
Non-200 response:
"
+
str
(
response
.
text
))
raise
Exception
(
"
Non-200 response:
"
+
str
(
response
.
text
))
data
=
response
.
json
()
data
=
response
.
json
()
for
i
,
image
in
enumerate
(
data
[
"
artifacts
"
]):
for
i
,
image
in
enumerate
(
data
[
"
artifacts
"
]):
with
open
(
f
"
./out/v1_txt2img_
{
i
}
.png
"
,
"
wb
"
)
as
f
:
with
open
(
f
"
./out/v1_txt2img_
{
i
}
.png
"
,
"
wb
"
)
as
f
:
f
.
write
(
base64
.
b64decode
(
image
[
"
base64
"
]))
f
.
write
(
base64
.
b64decode
(
image
[
"
base64
"
]))
```
```
%% Cell type:markdown id: tags:
# OpenAI / ChatGPT
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
!
pip
install
openai
```
```
%% Cell type:code id: tags:
```
python
import
os
import
openai
text
=
"
Generate 5 random scenes for some stock images as a csv
"
# Load your API key from an environment variable or secret management service
openai
.
api_key
=
'
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
'
#'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'
response
=
openai
.
Completion
.
create
(
model
=
"
text-davinci-003
"
,
prompt
=
text
,
temperature
=
0
,
max_tokens
=
150
)
answers
=
response
.
choices
[
0
].
text
print
(
response
);
print
(
response
.
choices
[
0
].
text
);
```
%% Output
random_scenes = [
["A woman in a white dress standing in a field of sunflowers",
"A man in a suit walking down a busy city street",
"A couple embracing on a beach at sunset",
"A family of four having a picnic in a park",
"A group of friends laughing around a campfire"],
["A woman in a red dress dancing in a nightclub",
"A man in a suit walking through a crowded market",
"A couple walking hand in hand through a forest",
"A family of four playing in a playground",
"A group of friends enjoying a sunset boat ride"],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment