Spaces:
Running
Running
Sunil Surendra Singh
commited on
Commit
·
265c07a
1
Parent(s):
f944fa7
added MongoDB support
Browse files- app.py +31 -14
- assets/app-design.png +2 -2
- config.py +10 -4
- model.py +10 -2
- mongo_utils.py +57 -0
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import model
|
3 |
from config import app_config
|
|
|
4 |
|
5 |
|
6 |
def clear():
|
@@ -29,19 +30,34 @@ def create_interface():
|
|
29 |
api_name=False,
|
30 |
)
|
31 |
with gr.Row():
|
32 |
-
gr.
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Row():
|
46 |
with gr.Column():
|
47 |
image = gr.Image(
|
@@ -89,7 +105,7 @@ def create_interface():
|
|
89 |
submit_button.click(
|
90 |
fn=model.generate_story,
|
91 |
inputs=[image, word_count, creativity],
|
92 |
-
outputs=[story],
|
93 |
)
|
94 |
clear_button.click(
|
95 |
fn=clear, inputs=[], outputs=[image, word_count, creativity, story]
|
@@ -100,5 +116,6 @@ def create_interface():
|
|
100 |
|
101 |
|
102 |
if __name__ == "__main__":
|
|
|
103 |
app = create_interface()
|
104 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import model
|
3 |
from config import app_config
|
4 |
+
import mongo_utils as mongo
|
5 |
|
6 |
|
7 |
def clear():
|
|
|
30 |
api_name=False,
|
31 |
)
|
32 |
with gr.Row():
|
33 |
+
with gr.Column(scale=5):
|
34 |
+
gr.Markdown(
|
35 |
+
"""
|
36 |
+
# Storyteller
|
37 |
+
**This app can craft captivating narratives from captivating images,
|
38 |
+
potentially surpassing even Shakespearean standards. Select an image
|
39 |
+
that inspires a story, choose a story length (up to 100 words), and
|
40 |
+
adjust the creativity index to enhance its creative flair.**
|
41 |
+
<br>
|
42 |
+
***Please exercise patience, as the models employed are extensive and may
|
43 |
+
require a few seconds to load. If you encounter an unrelated story,
|
44 |
+
it is likely still loading; wait a moment and try again.***
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
with gr.Column(scale=2):
|
48 |
+
max_count = gr.Textbox(
|
49 |
+
label="Max allowed OpenAI requests:",
|
50 |
+
value=app_config.openai_max_access_count,
|
51 |
+
)
|
52 |
+
curr_count = gr.Textbox(
|
53 |
+
label="Used up OpenAI requests:",
|
54 |
+
value=app_config.openai_curr_access_count,
|
55 |
+
)
|
56 |
+
available_count = gr.Textbox(
|
57 |
+
label="Available OpenAI requests:",
|
58 |
+
value=app_config.openai_max_access_count
|
59 |
+
- app_config.openai_curr_access_count,
|
60 |
+
)
|
61 |
with gr.Row():
|
62 |
with gr.Column():
|
63 |
image = gr.Image(
|
|
|
105 |
submit_button.click(
|
106 |
fn=model.generate_story,
|
107 |
inputs=[image, word_count, creativity],
|
108 |
+
outputs=[story, max_count, curr_count, available_count],
|
109 |
)
|
110 |
clear_button.click(
|
111 |
fn=clear, inputs=[], outputs=[image, word_count, creativity, story]
|
|
|
116 |
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
+
mongo.fetch_curr_access_count()
|
120 |
app = create_interface()
|
121 |
app.launch()
|
assets/app-design.png
CHANGED
Git LFS Details
|
Git LFS Details
|
config.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
from dataclasses import dataclass
|
3 |
-
import gradio as gr
|
4 |
|
5 |
|
6 |
@dataclass
|
@@ -8,9 +7,16 @@ class AppConfig:
|
|
8 |
title = "Picture to Story Generator"
|
9 |
theme = "freddyaboulton/dracula_revamped"
|
10 |
css = "style.css"
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
|
16 |
app_config = AppConfig()
|
|
|
1 |
import os
|
2 |
from dataclasses import dataclass
|
|
|
3 |
|
4 |
|
5 |
@dataclass
|
|
|
7 |
title = "Picture to Story Generator"
|
8 |
theme = "freddyaboulton/dracula_revamped"
|
9 |
css = "style.css"
|
10 |
+
openai_max_access_count = 200
|
11 |
+
openai_curr_access_count = None
|
12 |
+
mongo_client = None
|
13 |
+
db = "mydb"
|
14 |
+
collection = "pic2story-openai-access-counter"
|
15 |
+
key = "current_count"
|
16 |
+
# HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
+
# OPENAI_KEY = os.getenv("OPENAI_KEY")
|
18 |
+
# I2T_API_URL = os.getenv("I2T_API_URL")
|
19 |
+
# MONGO_CONN_STR = os.getenv("MONGO_CONN_STR")
|
20 |
|
21 |
|
22 |
app_config = AppConfig()
|
model.py
CHANGED
@@ -8,6 +8,7 @@ from langchain.prompts import (
|
|
8 |
ChatPromptTemplate,
|
9 |
)
|
10 |
from config import app_config
|
|
|
11 |
|
12 |
|
13 |
def __image2text(image):
|
@@ -51,11 +52,18 @@ def __text2story(image_desc, word_count, creativity):
|
|
51 |
|
52 |
def generate_story(image_file, word_count, creativity):
|
53 |
"""Generates a story given an image"""
|
54 |
-
# read image as bytes
|
55 |
with open(image_file, "rb") as f:
|
56 |
input_image = f.read()
|
|
|
57 |
image_desc = __image2text(image=input_image)
|
|
|
58 |
story = __text2story(
|
59 |
image_desc=image_desc, word_count=word_count, creativity=creativity
|
60 |
)
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
8 |
ChatPromptTemplate,
|
9 |
)
|
10 |
from config import app_config
|
11 |
+
import mongo_utils as mongo
|
12 |
|
13 |
|
14 |
def __image2text(image):
|
|
|
52 |
|
53 |
def generate_story(image_file, word_count, creativity):
|
54 |
"""Generates a story given an image"""
|
55 |
+
# read image as bytes arrayS
|
56 |
with open(image_file, "rb") as f:
|
57 |
input_image = f.read()
|
58 |
+
# generate caption for image
|
59 |
image_desc = __image2text(image=input_image)
|
60 |
+
# generate story from caption
|
61 |
story = __text2story(
|
62 |
image_desc=image_desc, word_count=word_count, creativity=creativity
|
63 |
)
|
64 |
+
# increment the openai access counter and compute count stats
|
65 |
+
mongo.increment_curr_access_count()
|
66 |
+
max_count = app_config.openai_max_access_count
|
67 |
+
curr_count = app_config.openai_curr_access_count
|
68 |
+
available_count = max_count - curr_count
|
69 |
+
return story, max_count, curr_count, available_count
|
mongo_utils.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pymongo
|
2 |
+
from config import app_config
|
3 |
+
|
4 |
+
|
5 |
+
# Generic functions
|
6 |
+
def get_db_client():
|
7 |
+
"""Returns MongoDB client object, connect to MongoDB Atlas instances if required"""
|
8 |
+
try:
|
9 |
+
if app_config.mongo_client == None:
|
10 |
+
client = pymongo.MongoClient(app_config.MONGO_CONN_STR)
|
11 |
+
app_config.mongo_client = client
|
12 |
+
except Exception as e:
|
13 |
+
print(e)
|
14 |
+
return app_config.mongo_client
|
15 |
+
|
16 |
+
|
17 |
+
def fetch_document(client, db, collection):
|
18 |
+
"""Get a single document from the provided db and collection"""
|
19 |
+
try:
|
20 |
+
document = client[db][collection].find_one()
|
21 |
+
except Exception as e:
|
22 |
+
print(e)
|
23 |
+
return document
|
24 |
+
|
25 |
+
|
26 |
+
def update_document(client, db, collection, key, value):
|
27 |
+
"""Update the passed key in the document for provided db and collection"""
|
28 |
+
try:
|
29 |
+
document = fetch_document(client, db, collection)
|
30 |
+
client[db][collection].update_one(
|
31 |
+
{"_id": document["_id"]},
|
32 |
+
{"$set": {key: value}},
|
33 |
+
)
|
34 |
+
except Exception as e:
|
35 |
+
print(e)
|
36 |
+
|
37 |
+
|
38 |
+
# Use case specific functions
|
39 |
+
def fetch_curr_access_count():
|
40 |
+
client = get_db_client()
|
41 |
+
curr_count = fetch_document(
|
42 |
+
client=client, db=app_config.db, collection=app_config.collection
|
43 |
+
)[app_config.key]
|
44 |
+
app_config.openai_curr_access_count = curr_count
|
45 |
+
|
46 |
+
|
47 |
+
def increment_curr_access_count():
|
48 |
+
client = get_db_client()
|
49 |
+
updated_count = app_config.openai_curr_access_count + 1
|
50 |
+
update_document(
|
51 |
+
client=client,
|
52 |
+
db=app_config.db,
|
53 |
+
collection=app_config.collection,
|
54 |
+
key=app_config.key,
|
55 |
+
value=updated_count,
|
56 |
+
)
|
57 |
+
app_config.openai_curr_access_count = updated_count
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
gradio==3.45.*
|
2 |
requests==2.29.*
|
3 |
langchain==0.0.*
|
4 |
-
openai==0.27.*
|
|
|
|
1 |
gradio==3.45.*
|
2 |
requests==2.29.*
|
3 |
langchain==0.0.*
|
4 |
+
openai==0.27.*
|
5 |
+
pymongo[tls, srv]==4.4.*
|