Spaces:
Runtime error
Runtime error
Commit
Β·
151e0a5
1
Parent(s):
7882fdb
upgraded GPT to GPT-4
Browse files- __pycache__/story_generator.cpython-310.pyc +0 -0
- app.py +27 -4
- story_generator.py +0 -20
__pycache__/story_generator.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/story_generator.cpython-310.pyc and b/__pycache__/story_generator.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -8,6 +8,8 @@ from diffusers import StableDiffusionPipeline
|
|
| 8 |
import gradio as gr
|
| 9 |
from story_generator import StoryGenerator
|
| 10 |
import torch
|
|
|
|
|
|
|
| 11 |
|
| 12 |
TITLE = '# Fictionista π§'
|
| 13 |
DESCRIPTION = '''#
|
|
@@ -60,7 +62,7 @@ with gr.Blocks(css='style.css') as image_gen_block:
|
|
| 60 |
label='Angle')
|
| 61 |
run_button = gr.Button('Run')
|
| 62 |
with gr.Column():
|
| 63 |
-
result = gr.Image(label='Result', elem_id='
|
| 64 |
|
| 65 |
# City generation tab
|
| 66 |
with gr.TabItem('City'):
|
|
@@ -82,9 +84,30 @@ with gr.Blocks(css='style.css') as image_gen_block:
|
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
-
def generate_story(prompt,
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def generate_city():
|
| 90 |
city_prompt = f"render 2048K ultra realistic render high definition a very technologically advanced city, cyberpunk, hyper realistic, detailed, ray tracing, POV-Ray, metallic, Sci-Fi"
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
from story_generator import StoryGenerator
|
| 10 |
import torch
|
| 11 |
+
import requests
|
| 12 |
+
import json
|
| 13 |
|
| 14 |
TITLE = '# Fictionista π§'
|
| 15 |
DESCRIPTION = '''#
|
|
|
|
| 62 |
label='Angle')
|
| 63 |
run_button = gr.Button('Run')
|
| 64 |
with gr.Column():
|
| 65 |
+
result = gr.Image(label='Result', elem_id='Character')
|
| 66 |
|
| 67 |
# City generation tab
|
| 68 |
with gr.TabItem('City'):
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
+
def generate_story(prompt, API_KEY, model="gpt-4", temperature=1, max_tokens=None):
|
| 88 |
+
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
| 89 |
+
|
| 90 |
+
headers = {
|
| 91 |
+
"Content-Type": "application/json",
|
| 92 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 93 |
+
}
|
| 94 |
+
prompt = [{"role": "user", "content":prompt}]
|
| 95 |
+
data = {
|
| 96 |
+
"model": model,
|
| 97 |
+
"messages": prompt,
|
| 98 |
+
"temperature": temperature,
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
if max_tokens is not None:
|
| 102 |
+
data["max_tokens"] = max_tokens
|
| 103 |
+
|
| 104 |
+
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(data))
|
| 105 |
+
|
| 106 |
+
if response.status_code == 200:
|
| 107 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 108 |
+
else:
|
| 109 |
+
raise Exception(f"Error {response.status_code}: {response.text}")
|
| 110 |
+
|
| 111 |
|
| 112 |
def generate_city():
|
| 113 |
city_prompt = f"render 2048K ultra realistic render high definition a very technologically advanced city, cyberpunk, hyper realistic, detailed, ray tracing, POV-Ray, metallic, Sci-Fi"
|
story_generator.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
|
| 3 |
-
class StoryGenerator:
|
| 4 |
-
def __init__(self, api_key):
|
| 5 |
-
|
| 6 |
-
openai.api_key = api_key
|
| 7 |
-
self.completion_model = "text-davinci-002"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def generate_story(self, prompt):
|
| 11 |
-
prompt = f"Once upon a time, {prompt}"
|
| 12 |
-
response = openai.Completion.create(
|
| 13 |
-
engine=self.completion_model,
|
| 14 |
-
prompt=prompt,
|
| 15 |
-
max_tokens=1024,
|
| 16 |
-
n=1,
|
| 17 |
-
stop=None,
|
| 18 |
-
temperature=0.5,
|
| 19 |
-
)
|
| 20 |
-
return response.choices[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|