crude version, but working
Browse files- .vscode/settings.json +5 -0
- app.py +117 -15
.vscode/settings.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"editor.formatOnPaste": true,
|
| 3 |
+
"editor.detectIndentation": false,
|
| 4 |
+
"python.formatting.provider": "black"
|
| 5 |
+
}
|
app.py
CHANGED
|
@@ -1,18 +1,121 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
|
|
|
| 4 |
import random
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from openai import OpenAI
|
|
|
|
|
|
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 11 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 12 |
-
print(openai.api_key)
|
| 13 |
-
print (os.environ.get("OPENAI_API_KEY"))
|
| 14 |
client = openai.OpenAI(organization='org-eiNl8e4nk93VLQFDb4EBz9JG')
|
| 15 |
#openai.api_key = os.environ['openai'] #for huggingface i think
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
#generates an AI description of your character
|
| 18 |
def describe(names,wis,char,str,int,dex,con):
|
|
@@ -43,21 +146,19 @@ def describe(names,wis,char,str,int,dex,con):
|
|
| 43 |
if not result :
|
| 44 |
result = "Could you be any more boring?"
|
| 45 |
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
#generates an AI description of your character
|
| 50 |
def makeName():
|
| 51 |
|
| 52 |
-
prom=f"We're generating Dungeons and Dragons characters for a new campaign, and we need a name."
|
| 53 |
-
prom+=f" Please imagine and give me just a character name for the player to use."
|
| 54 |
completion = client.completions.create(
|
| 55 |
model='gpt-3.5-turbo-instruct',
|
| 56 |
prompt=prom,
|
| 57 |
max_tokens=50,
|
| 58 |
-
temperature=0.
|
| 59 |
-
frequency_penalty=0.2,
|
| 60 |
-
presence_penalty= 0.25)
|
| 61 |
|
| 62 |
result =completion.choices[0].text
|
| 63 |
#print(dict(completion).get('usage'))
|
|
@@ -69,7 +170,7 @@ def makeName():
|
|
| 69 |
result = "The Doctor"
|
| 70 |
|
| 71 |
|
| 72 |
-
return result
|
| 73 |
|
| 74 |
def stat( ndice, dicerank):
|
| 75 |
# return ndice + dicerank
|
|
@@ -101,13 +202,14 @@ with gr.Blocks() as statBlock:
|
|
| 101 |
|
| 102 |
rollButt = gr.Button(value="Roll Stats")
|
| 103 |
describeButt = gr.Button(value="Generate Description")
|
| 104 |
-
|
| 105 |
outputBox=gr.Textbox(label="The character",show_label=True)
|
| 106 |
-
|
|
|
|
| 107 |
nameButt.click(makeName,inputs=[],outputs=[nameBox])
|
| 108 |
rollButt.click(stat, inputs=[dice,gr.Number(value=6, visible=False,precision=0)],outputs=[wisBox,charBox,strBox,intBox,dexBox,conBox])
|
| 109 |
describeButt.click(describe, outputs=[outputBox],inputs=[nameBox,wisBox,charBox,strBox,intBox,dexBox,conBox])
|
|
|
|
| 110 |
statBlock.launch()
|
| 111 |
|
| 112 |
|
| 113 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
+
import IPython
|
| 5 |
import random
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
import time
|
| 9 |
+
import math
|
| 10 |
+
import io
|
| 11 |
+
|
| 12 |
+
from stability_sdk import client as sclient
|
| 13 |
+
from io import BytesIO
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
from openai import OpenAI
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
| 18 |
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
+
sdurl = "https://stablediffusionapi.com/api/v3/text2img"
|
| 22 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 23 |
+
#print(openai.api_key)
|
| 24 |
+
#print (os.environ.get("OPENAI_API_KEY"))
|
| 25 |
client = openai.OpenAI(organization='org-eiNl8e4nk93VLQFDb4EBz9JG')
|
| 26 |
#openai.api_key = os.environ['openai'] #for huggingface i think
|
| 27 |
+
os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
|
| 28 |
+
stability_api = sclient.StabilityInference(
|
| 29 |
+
key=os.getenv('STABILITY_KEY'),
|
| 30 |
+
verbose=True,
|
| 31 |
+
engine="stable-diffusion-xl-1024-v1-0"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
#generates an AI picture of your char
|
| 37 |
+
def draw(thechar):
|
| 38 |
+
thechar=thechar.split("$$$")[0].strip()
|
| 39 |
+
moretime=50
|
| 40 |
+
cfg=8.0
|
| 41 |
+
noinit=0
|
| 42 |
+
r2="1"
|
| 43 |
+
r3="1"
|
| 44 |
+
filename="drunkgrogu"+str(r2)+".png"
|
| 45 |
+
filenamefilter="calvin"+str(r3)+".png"
|
| 46 |
+
#await ctx.channel.send(file=discord.File(filename))
|
| 47 |
+
#stable-diffusion-xl-1024-v0-9
|
| 48 |
+
notoldguy="bad anatomy, fuzzy, out of focus, cut off, low res"
|
| 49 |
+
|
| 50 |
+
try :
|
| 51 |
+
if noinit :
|
| 52 |
+
print("initimg")
|
| 53 |
+
answers = stability_api.generate(
|
| 54 |
+
prompt=[generation.Prompt(text=thechar,parameters=generation.PromptParameters(weight=1)),
|
| 55 |
+
generation.Prompt(text=notoldguy,parameters=generation.PromptParameters(weight=-1))],
|
| 56 |
+
steps=moretime, # defaults to 50 if not specified
|
| 57 |
+
)
|
| 58 |
+
else :
|
| 59 |
+
print("noinitimg:"+thechar+"\nneg:"+notoldguy)
|
| 60 |
+
answers = stability_api.generate(
|
| 61 |
+
prompt=[generation.Prompt(text=thechar,parameters=generation.PromptParameters(weight=1)),
|
| 62 |
+
generation.Prompt(text=notoldguy,parameters=generation.PromptParameters(weight=-1))],
|
| 63 |
+
cfg_scale=cfg,
|
| 64 |
+
steps=moretime, # defaults to 50 if not specified
|
| 65 |
+
width=1024,
|
| 66 |
+
height=1024
|
| 67 |
+
)
|
| 68 |
+
except Exception as eeeee :
|
| 69 |
+
print(eeeee)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
try :
|
| 73 |
+
# iterating over the generator produces the api response
|
| 74 |
+
for resp in answers:
|
| 75 |
+
#print(resp)
|
| 76 |
+
for artifact in resp.artifacts:
|
| 77 |
+
#print("fin rease:"+str(artifact.finish_reason))
|
| 78 |
+
if artifact.finish_reason == generation.FILTER:
|
| 79 |
+
if random.randrange(0,2)==1 :
|
| 80 |
+
print("await ctx.channel.send(file=discord.File(filenamefilter))")
|
| 81 |
+
else :
|
| 82 |
+
print(" await ctx.channel.send(file=discord.File(filename))")
|
| 83 |
+
#warnings.warn("Your request activated the API's safety filters and could not be processed. Please modify the prompt and try again.")
|
| 84 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 85 |
+
#print("we got an image")
|
| 86 |
+
img = Image.open(io.BytesIO(artifact.binary))
|
| 87 |
+
#oldoldguy=oldoldguy.replace("|","--")
|
| 88 |
+
# oldoldguy = oldoldguy.replace("*","")
|
| 89 |
+
#metadata = PngInfo()
|
| 90 |
+
#metadata.add_text("prompt", oldguy)
|
| 91 |
+
thechar=thechar.replace("Character Description","")
|
| 92 |
+
filename="dndChar"+str(math.trunc(time.time()))+thechar[:80].replace("\n","").replace(",","")
|
| 93 |
+
|
| 94 |
+
filename=filename[:245]+".png"
|
| 95 |
+
img.save(filename)
|
| 96 |
+
|
| 97 |
+
##filename="image5r"+str(math.trunc(time.time()))+".png"
|
| 98 |
+
#print(str(img))
|
| 99 |
+
##filename=filename[:245]+".png"
|
| 100 |
+
#print(filename)
|
| 101 |
+
##img.save(filename)
|
| 102 |
+
#filename="image5r"+str(math.trunc(time.time()))+".png"
|
| 103 |
+
#img.save(filename)
|
| 104 |
+
#display(img)
|
| 105 |
+
|
| 106 |
+
except Exception as eeee :
|
| 107 |
+
|
| 108 |
+
print(eeee.details)
|
| 109 |
+
print("Error. Have a random image instead:");
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
#print(response.text)
|
| 113 |
+
#print(response.output)
|
| 114 |
+
print(filename)
|
| 115 |
+
imageBox = gr.Image(value=Image.open(filename))
|
| 116 |
+
imageBox2 = img
|
| 117 |
+
return img
|
| 118 |
+
|
| 119 |
|
| 120 |
#generates an AI description of your character
|
| 121 |
def describe(names,wis,char,str,int,dex,con):
|
|
|
|
| 146 |
if not result :
|
| 147 |
result = "Could you be any more boring?"
|
| 148 |
|
| 149 |
+
#draw(result.split("$$$")[0],outputs=imageBox2)
|
| 150 |
+
return result#result.replace("$$$","")
|
| 151 |
|
| 152 |
+
#generates an AI nam,
|
|
|
|
|
|
|
| 153 |
def makeName():
|
| 154 |
|
| 155 |
+
prom=f"We're generating Dungeons and Dragons characters for a new campaign, and we need a good, original name."
|
| 156 |
+
prom+=f" Please imagine and give me just a character name for the player to use. Be creative and fun. No duds."
|
| 157 |
completion = client.completions.create(
|
| 158 |
model='gpt-3.5-turbo-instruct',
|
| 159 |
prompt=prom,
|
| 160 |
max_tokens=50,
|
| 161 |
+
temperature=0.97)
|
|
|
|
|
|
|
| 162 |
|
| 163 |
result =completion.choices[0].text
|
| 164 |
#print(dict(completion).get('usage'))
|
|
|
|
| 170 |
result = "The Doctor"
|
| 171 |
|
| 172 |
|
| 173 |
+
return result.strip()
|
| 174 |
|
| 175 |
def stat( ndice, dicerank):
|
| 176 |
# return ndice + dicerank
|
|
|
|
| 202 |
|
| 203 |
rollButt = gr.Button(value="Roll Stats")
|
| 204 |
describeButt = gr.Button(value="Generate Description")
|
| 205 |
+
imgButt = gr.Button(value="Generate Image")
|
| 206 |
outputBox=gr.Textbox(label="The character",show_label=True)
|
| 207 |
+
imageBox = gr.Image()
|
| 208 |
+
imageBox2 = gr.Image()
|
| 209 |
nameButt.click(makeName,inputs=[],outputs=[nameBox])
|
| 210 |
rollButt.click(stat, inputs=[dice,gr.Number(value=6, visible=False,precision=0)],outputs=[wisBox,charBox,strBox,intBox,dexBox,conBox])
|
| 211 |
describeButt.click(describe, outputs=[outputBox],inputs=[nameBox,wisBox,charBox,strBox,intBox,dexBox,conBox])
|
| 212 |
+
imgButt.click(draw,inputs=[outputBox],outputs=[imageBox2])
|
| 213 |
statBlock.launch()
|
| 214 |
|
| 215 |
|
|
|