Spaces:
Runtime error
Runtime error
add db connection and prompt logging
Browse files- app.py +26 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import gradio as gr
|
|
| 5 |
from openai import OpenAI
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
openai_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -22,11 +25,22 @@ if pw_key == "":
|
|
| 22 |
if openai_key == "":
|
| 23 |
sys.exit("Please Provide Your OpenAI API Key")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def generate_image(text, pw, model):
|
| 28 |
# add a conditional to check for a valid password
|
| 29 |
-
|
| 30 |
if pw != os.getenv("PW"):
|
| 31 |
# output an error message to the user in the gradio interface if password is invalid
|
| 32 |
raise gr.Error("Invalid password. Please try again.")
|
|
@@ -34,6 +48,8 @@ def generate_image(text, pw, model):
|
|
| 34 |
try:
|
| 35 |
client = OpenAI(api_key=openai_key)
|
| 36 |
|
|
|
|
|
|
|
| 37 |
response = client.images.generate(
|
| 38 |
prompt=text,
|
| 39 |
model=model, # dall-e-2 or dall-e-3
|
|
@@ -45,7 +61,15 @@ def generate_image(text, pw, model):
|
|
| 45 |
print(str(error))
|
| 46 |
raise gr.Error("An error occurred while generating the image.")
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
with gr.Blocks() as demo:
|
|
|
|
| 5 |
from openai import OpenAI
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
+
from pymongo.mongo_client import MongoClient
|
| 9 |
+
from pymongo.server_api import ServerApi
|
| 10 |
+
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
openai_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 25 |
if openai_key == "":
|
| 26 |
sys.exit("Please Provide Your OpenAI API Key")
|
| 27 |
|
| 28 |
+
# Connect to MongoDB
|
| 29 |
+
uri = os.getenv("MONGO_URI")
|
| 30 |
+
mongo_client = MongoClient(uri, server_api=ServerApi('1'))
|
| 31 |
+
|
| 32 |
+
mongo_db = mongo_client.pdr
|
| 33 |
+
mongo_collection = mongo_db["images"]
|
| 34 |
|
| 35 |
+
# Send a ping to confirm a successful connection
|
| 36 |
+
try:
|
| 37 |
+
mongo_client.admin.command('ping')
|
| 38 |
+
print("Pinged your deployment. You successfully connected to MongoDB!")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(e)
|
| 41 |
|
| 42 |
def generate_image(text, pw, model):
|
| 43 |
# add a conditional to check for a valid password
|
|
|
|
| 44 |
if pw != os.getenv("PW"):
|
| 45 |
# output an error message to the user in the gradio interface if password is invalid
|
| 46 |
raise gr.Error("Invalid password. Please try again.")
|
|
|
|
| 48 |
try:
|
| 49 |
client = OpenAI(api_key=openai_key)
|
| 50 |
|
| 51 |
+
|
| 52 |
+
|
| 53 |
response = client.images.generate(
|
| 54 |
prompt=text,
|
| 55 |
model=model, # dall-e-2 or dall-e-3
|
|
|
|
| 61 |
print(str(error))
|
| 62 |
raise gr.Error("An error occurred while generating the image.")
|
| 63 |
|
| 64 |
+
image_url = response.data[0].url
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
mongo_collection.insert_one({"text": text, "model": model, "image_url": image_url})
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(e)
|
| 70 |
+
raise gr.Error("An error occurred while saving the prompt to the database.")
|
| 71 |
+
|
| 72 |
+
return image_url
|
| 73 |
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gradio==4.2.0
|
| 2 |
openai==1.2.3
|
| 3 |
-
python-dotenv==1.0.0
|
|
|
|
|
|
| 1 |
gradio==4.2.0
|
| 2 |
openai==1.2.3
|
| 3 |
+
python-dotenv==1.0.0
|
| 4 |
+
pymongo
|