Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,33 @@ import gradio as gr
|
|
| 8 |
import spaces
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
|
| 10 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Constants
|
| 13 |
MAX_MAX_NEW_TOKENS = 2048
|
|
@@ -124,16 +151,19 @@ def generate(
|
|
| 124 |
output = "".join(outputs)
|
| 125 |
yield output
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
# try:
|
| 129 |
# saved_story = Story(message=message, content=final_story).save()
|
| 130 |
# yield f"{final_story}\n\n Story saved with ID: {saved_story.story_id}"
|
| 131 |
# except Exception as e:
|
| 132 |
# yield f"Failed to save story: {str(e)}"
|
|
|
|
| 133 |
|
| 134 |
# Gradio Interface Setup
|
| 135 |
chat_interface = gr.ChatInterface(
|
| 136 |
fn=generate,
|
|
|
|
| 137 |
fill_height=True,
|
| 138 |
stop_btn=None,
|
| 139 |
examples=[
|
|
@@ -146,7 +176,7 @@ chat_interface = gr.ChatInterface(
|
|
| 146 |
|
| 147 |
# Gradio Web Interface
|
| 148 |
with gr.Blocks(css=custom_css,theme='shivi/calm_seafoam',fill_height=True) as demo:
|
| 149 |
-
|
| 150 |
chat_interface.render()
|
| 151 |
# gr.Markdown(LICENSE)
|
| 152 |
|
|
|
|
| 8 |
import spaces
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer
|
| 10 |
from peft import PeftModel
|
| 11 |
+
from openai import OpenAI
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
openai_key = os.getenv("OPENAI_KEY")
|
| 17 |
+
|
| 18 |
+
if openai_key == "":
|
| 19 |
+
sys.exit("Please Provide Your OpenAI API Key")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def generate_image(text):
|
| 23 |
+
try:
|
| 24 |
+
client = OpenAI(api_key=openai_key)
|
| 25 |
+
|
| 26 |
+
response = client.images.generate(
|
| 27 |
+
model="dall-e-3",
|
| 28 |
+
prompt="Create an illustration that accurately depicts the character and the setting of this story:"+text,
|
| 29 |
+
n=1,
|
| 30 |
+
size="1024x1024"
|
| 31 |
+
)
|
| 32 |
+
except Exception as error:
|
| 33 |
+
print(str(error))
|
| 34 |
+
raise gr.Error("An error occurred while generating speech. Please check your API key and come back try again.")
|
| 35 |
+
|
| 36 |
+
return response.data[0].url
|
| 37 |
+
|
| 38 |
|
| 39 |
# Constants
|
| 40 |
MAX_MAX_NEW_TOKENS = 2048
|
|
|
|
| 151 |
output = "".join(outputs)
|
| 152 |
yield output
|
| 153 |
|
| 154 |
+
final_story = "".join(outputs)
|
| 155 |
+
generate_image(final_story)
|
| 156 |
# try:
|
| 157 |
# saved_story = Story(message=message, content=final_story).save()
|
| 158 |
# yield f"{final_story}\n\n Story saved with ID: {saved_story.story_id}"
|
| 159 |
# except Exception as e:
|
| 160 |
# yield f"Failed to save story: {str(e)}"
|
| 161 |
+
|
| 162 |
|
| 163 |
# Gradio Interface Setup
|
| 164 |
chat_interface = gr.ChatInterface(
|
| 165 |
fn=generate,
|
| 166 |
+
label=" ",
|
| 167 |
fill_height=True,
|
| 168 |
stop_btn=None,
|
| 169 |
examples=[
|
|
|
|
| 176 |
|
| 177 |
# Gradio Web Interface
|
| 178 |
with gr.Blocks(css=custom_css,theme='shivi/calm_seafoam',fill_height=True) as demo:
|
| 179 |
+
output_image = gr.Image(label="Image Output")
|
| 180 |
chat_interface.render()
|
| 181 |
# gr.Markdown(LICENSE)
|
| 182 |
|