Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,26 +10,33 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
|
| 10 |
from peft import PeftModel
|
| 11 |
import openai
|
| 12 |
from openai import OpenAI
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
openai.api_key = os.environ.get("OPENAI_KEY")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_image(text):
|
| 19 |
try:
|
|
|
|
| 20 |
response = openai.images.generate(
|
| 21 |
model="dall-e-3",
|
| 22 |
-
prompt="Create an illustration that accurately depicts the character and the setting of a story:"+text,
|
| 23 |
n=1,
|
| 24 |
size="1024x1024"
|
| 25 |
)
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as error:
|
| 27 |
-
|
| 28 |
raise gr.Error("An error occurred while generating the image. Please check your API key and try again.")
|
| 29 |
|
| 30 |
|
| 31 |
-
return response.data[0].url
|
| 32 |
-
|
| 33 |
|
| 34 |
# Constants
|
| 35 |
MAX_MAX_NEW_TOKENS = 2048
|
|
@@ -99,6 +106,8 @@ def generate(
|
|
| 99 |
top_k: int = 20,
|
| 100 |
repetition_penalty: float = 1.0,
|
| 101 |
) -> Iterator[str]:
|
|
|
|
|
|
|
| 102 |
conversation = []
|
| 103 |
for user, assistant in chat_history:
|
| 104 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
|
|
|
| 10 |
from peft import PeftModel
|
| 11 |
import openai
|
| 12 |
from openai import OpenAI
|
| 13 |
+
import logging
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
openai.api_key = os.environ.get("OPENAI_KEY")
|
| 18 |
|
| 19 |
+
# Set up logging configuration
|
| 20 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 21 |
+
|
| 22 |
+
# Example usage of logging in your function
|
| 23 |
def generate_image(text):
|
| 24 |
try:
|
| 25 |
+
logging.debug("Generating image with prompt: %s", text)
|
| 26 |
response = openai.images.generate(
|
| 27 |
model="dall-e-3",
|
| 28 |
+
prompt="Create an illustration that accurately depicts the character and the setting of a story:" + text,
|
| 29 |
n=1,
|
| 30 |
size="1024x1024"
|
| 31 |
)
|
| 32 |
+
image_url = response.data[0].url
|
| 33 |
+
logging.info("Image generated successfully: %s", image_url)
|
| 34 |
+
return image_url
|
| 35 |
except Exception as error:
|
| 36 |
+
logging.error("Failed to generate image: %s", str(error))
|
| 37 |
raise gr.Error("An error occurred while generating the image. Please check your API key and try again.")
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Constants
|
| 42 |
MAX_MAX_NEW_TOKENS = 2048
|
|
|
|
| 106 |
top_k: int = 20,
|
| 107 |
repetition_penalty: float = 1.0,
|
| 108 |
) -> Iterator[str]:
|
| 109 |
+
logging.debug("Starting chat generation with message: %s", message)
|
| 110 |
+
|
| 111 |
conversation = []
|
| 112 |
for user, assistant in chat_history:
|
| 113 |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|