Spaces:
Build error
Build error
Upload 20 files
Browse files- .gitattributes +2 -0
- README.md +94 -12
- __pycache__/text_model.cpython-311.pyc +0 -0
- app.py +19 -0
- image.jpg +0 -0
- images/1353.jpg +0 -0
- images/1683386165361.jpeg +0 -0
- images/2048x1536-harley-davidson-riders-8k_1536316579.jpg +3 -0
- images/2048x1536-spiderman-miles-lost-in-space-4k_1553071367.jpg +0 -0
- images/4f503844d9a44b0350c25eeefae028d3.jpg +0 -0
- images/80g58cvhb7x31.jpg +0 -0
- images/IMG_2696.JPG +3 -0
- images/OIP.jpg +0 -0
- images/Screenshot 2022-09-29 234504.png +0 -0
- images/WhatsApp Image 2023-05-05 at 15.10.45.jpg +0 -0
- images/WhatsApp Image 2023-05-07 at 10.46.57.jpg +0 -0
- images/photo_2023-06-14_13-00-23.jpg +0 -0
- images/photo_2023-06-14_13-00-31.jpg +0 -0
- images/togather.jpg +0 -0
- text_model.py +51 -0
- wp5.jpg +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
images/2048x1536-harley-davidson-riders-8k_1536316579.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
images/IMG_2696.JPG filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1,94 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
https://github.com/arjunprakash027/Story_Teller/assets/72484657/1c756dd8-9889-420d-a30e-13dbe137a2f0
|
5 |
+
|
6 |
+
|
7 |
+
# Story Teller
|
8 |
+
|
9 |
+
Story Teller is a Streamlit application that generates a story based on an input image. It utilizes the Hugging Face Transformers library and the Salesforce BLIP Image Captioning model.
|
10 |
+
|
11 |
+
## Table of Contents
|
12 |
+
|
13 |
+
- [Installation](#installation)
|
14 |
+
- [Usage](#usage)
|
15 |
+
- [API Token](#api-token)
|
16 |
+
- [Models](#models)
|
17 |
+
- [Running the Application](#running-the-application)
|
18 |
+
- [Contributing](#contributing)
|
19 |
+
|
20 |
+
## Installation
|
21 |
+
|
22 |
+
To install the necessary dependencies, run the following command:
|
23 |
+
|
24 |
+
```shell
|
25 |
+
pip install -r requirements.txt
|
26 |
+
```
|
27 |
+
|
28 |
+
Make sure you have the required dependencies specified in the `requirements.txt` file.
|
29 |
+
|
30 |
+
## Usage
|
31 |
+
|
32 |
+
To use the application, follow the steps below:
|
33 |
+
|
34 |
+
1. Run the Streamlit application by executing the following command:
|
35 |
+
|
36 |
+
```shell
|
37 |
+
streamlit run app.py
|
38 |
+
```
|
39 |
+
|
40 |
+
2. Access the application through the provided URL in the console.
|
41 |
+
|
42 |
+
3. The application interface will appear with the title "Story Teller" and an instruction to "Upload an image and get a story".
|
43 |
+
|
44 |
+
4. Click on the "Upload your file here..." button to select an image file (supported formats: PNG, JPEG, JPG).
|
45 |
+
|
46 |
+
5. Once the image is uploaded, it will be displayed on the page.
|
47 |
+
|
48 |
+
6. The application will process the uploaded image using the Salesforce BLIP Image Captioning model and generate a textual description of the image.
|
49 |
+
|
50 |
+
7. The generated text will then be passed to the Hugging Face API to generate a story based on the text.
|
51 |
+
|
52 |
+
8. The application will display the generated story on the page.
|
53 |
+
|
54 |
+
9. If any errors occur during the process, an error message will be shown on the page, and you can try again.
|
55 |
+
|
56 |
+
## API Token
|
57 |
+
|
58 |
+
The application requires an API token from Hugging Face to access the story generation model. To obtain an API token, follow these steps:
|
59 |
+
|
60 |
+
1. Sign up or log in to your Hugging Face account at [https://huggingface.co/](https://huggingface.co/).
|
61 |
+
|
62 |
+
2. Once logged in, go to your account settings and navigate to the "API token" section.
|
63 |
+
|
64 |
+
3. Generate a new API token, copy it, and replace the `"your api key"` placeholder in the `Models` class of `text_model.py` with your actual API token.
|
65 |
+
|
66 |
+
## Models
|
67 |
+
|
68 |
+
The `Models` class in `text_model.py` encapsulates the functionality of the application. It contains the following methods:
|
69 |
+
|
70 |
+
- `__init__()`:
|
71 |
+
- Initializes the class and sets the API token and model ID.
|
72 |
+
|
73 |
+
- `img2text(url)`:
|
74 |
+
- Takes an image URL as input and uses the Salesforce BLIP Image Captioning model to convert the image into text. It returns the generated text.
|
75 |
+
|
76 |
+
- `story(payload)`:
|
77 |
+
- Takes a payload as input, which contains the generated text, and sends a request to the Hugging Face API to generate a story based on the text. It returns the generated story.
|
78 |
+
|
79 |
+
- `chain(payload, num=0)`:
|
80 |
+
- This method acts as a recursive function that generates a chain of stories. It takes a payload as input, which initially contains the generated text. It recursively calls the `story()` method and updates the payload until the desired number of stories (50 in this case) is generated. The progress bar is also updated accordingly.
|
81 |
+
|
82 |
+
## Running the Application
|
83 |
+
If you are curious and want to just try the backend models
|
84 |
+
execute the following command:
|
85 |
+
|
86 |
+
```shell
|
87 |
+
python text_model.py
|
88 |
+
```
|
89 |
+
|
90 |
+
Make sure you have the required dependencies installed, as mentioned in the installation section.
|
91 |
+
|
92 |
+
## Contributing
|
93 |
+
|
94 |
+
Contributions to the Story Teller application are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
|
__pycache__/text_model.cpython-311.pyc
ADDED
Binary file (3.46 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from text_model import Models
|
3 |
+
import os
|
4 |
+
|
5 |
+
st.title("Story Teller")
|
6 |
+
model = Models()
|
7 |
+
st.write("Upload an image and get a story")
|
8 |
+
uploaded_file = st.file_uploader("Upload your file here...",type=['png','jpeg','jpg'])
|
9 |
+
|
10 |
+
try:
|
11 |
+
if uploaded_file is not None:
|
12 |
+
st.image(uploaded_file)
|
13 |
+
with open(os.path.join("images",uploaded_file.name),"wb") as f:
|
14 |
+
f.write(uploaded_file.getbuffer())
|
15 |
+
response = model.chain(model.img2text(f'images/{uploaded_file.name}'))
|
16 |
+
st.write(response)
|
17 |
+
except Exception as e:
|
18 |
+
print(e)
|
19 |
+
st.write("error occured! please try again")
|
image.jpg
ADDED
images/1353.jpg
ADDED
images/1683386165361.jpeg
ADDED
images/2048x1536-harley-davidson-riders-8k_1536316579.jpg
ADDED
Git LFS Details
|
images/2048x1536-spiderman-miles-lost-in-space-4k_1553071367.jpg
ADDED
images/4f503844d9a44b0350c25eeefae028d3.jpg
ADDED
images/80g58cvhb7x31.jpg
ADDED
images/IMG_2696.JPG
ADDED
Git LFS Details
|
images/OIP.jpg
ADDED
images/Screenshot 2022-09-29 234504.png
ADDED
images/WhatsApp Image 2023-05-05 at 15.10.45.jpg
ADDED
images/WhatsApp Image 2023-05-07 at 10.46.57.jpg
ADDED
images/photo_2023-06-14_13-00-23.jpg
ADDED
images/photo_2023-06-14_13-00-31.jpg
ADDED
images/togather.jpg
ADDED
text_model.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import find_dotenv, load_dotenv
|
2 |
+
from transformers import pipeline
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import logging
|
6 |
+
import streamlit as st
|
7 |
+
requests.verify = False
|
8 |
+
|
9 |
+
load_dotenv(find_dotenv()) #access huggingface token
|
10 |
+
log = logging.getLogger("text_model.py")
|
11 |
+
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
|
12 |
+
|
13 |
+
class Models:
|
14 |
+
def __init__(self):
|
15 |
+
self.api_token = "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"
|
16 |
+
self.model_id = "openai-gpt"
|
17 |
+
def img2text(self,url):
|
18 |
+
image_to_text = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
|
19 |
+
text = image_to_text(url)
|
20 |
+
log.info("image to text model running")
|
21 |
+
return text[0]['generated_text']
|
22 |
+
|
23 |
+
def story(self,payload):
|
24 |
+
payload = json.dumps(payload)
|
25 |
+
headers = {"Authorization": f"Bearer {self.api_token}"}
|
26 |
+
url = f"https://api-inference.huggingface.co/models/{self.model_id}"
|
27 |
+
response = requests.post(url, headers=headers, data=payload)
|
28 |
+
log.info("story model running")
|
29 |
+
return response.json()[0]['generated_text']
|
30 |
+
|
31 |
+
status_bar = st.progress(0,text="progress will be shown here")
|
32 |
+
def chain(self,payload,num=0):
|
33 |
+
if num == 50:
|
34 |
+
self.status_bar.progress(100,text="story generated!!")
|
35 |
+
return payload
|
36 |
+
|
37 |
+
response = self.story(payload)
|
38 |
+
percent = int(((num+1)/50) * 100)
|
39 |
+
self.status_bar.progress(percent,text="generating story for you!!")
|
40 |
+
return self.chain(response,num+1)
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
|
49 |
+
model = Models()
|
50 |
+
response = model.chain(model.img2text("D:\image-story-teller\Story_Teller\image.jpg"))
|
51 |
+
print(response)
|
wp5.jpg
ADDED