GurpreetKJ commited on
Commit
7754806
·
1 Parent(s): e6005d7

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -118
app.py DELETED
@@ -1,118 +0,0 @@
1
- import os
2
- from dotenv import load_dotenv
3
- from transformers import pipeline
4
- from langchain import OpenAI, PromptTemplate, LLMChain
5
- import requests
6
- import streamlit as st
7
-
8
- load_dotenv()
9
- HUGGINGFACE_API_TOKEN = os.getenv('HUGGINGFACE_API_TOKEN')
10
- OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
11
-
12
- #img to text
13
-
14
- def image_to_text(image_url):
15
- """
16
- Generates context text from image using image-to-text transformer model.
17
-
18
- Parameters:
19
- image_url (str): URL of the image to be processed.
20
-
21
- Returns:
22
- str: Context text.
23
- """
24
- pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
25
- text = pipe(image_url)[0]['generated_text']
26
- print(text)
27
- return text
28
-
29
-
30
- #llm
31
- def text_to_story(text, model='gpt-3.5-turbo-instruct-0914', temperature=0.9):
32
- """
33
- Generates a short story based on input text by prompting OpenAI's language model.
34
-
35
- Args:
36
- text (str): Input text used as context for generating the story.
37
- model (str): OpenAI model to be used for story generation. Defaults to 'gpt-3.5-turbo-instruct-0914'.
38
- temperature (float): Controls the randomness of the generated story. Defaults to 0.9.
39
-
40
- Returns:
41
- str: Generated short story based on the input context.
42
- """
43
- llm = OpenAI(openai_api_key=os.getenv("OPEN_API_KEY"), model=model, temperature=temperature)
44
- prompt = PromptTemplate(
45
- input_variables = ['text'],
46
- template = '''
47
- You are a talented story teller who can create a story from a simple narrative./
48
- Create a story using the following scenario; the story should have be maximum 50 words long.
49
- context = {text}
50
- '''
51
- )
52
- chain = LLMChain(llm=llm, prompt=prompt)
53
- story = chain.predict(text=text)
54
-
55
- print(story)
56
- return story
57
-
58
-
59
-
60
- # text to speech
61
- def story_to_speech(story):
62
- """
63
- Generates and saves an audio file of speech narrating the short story using a text-to-speech tranformer model.
64
-
65
- Args:
66
- story (str): Short story to be converted to speech.
67
- """
68
- API_URL = 'https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits'
69
- headers = {"Authorization": f'Bearer {HUGGINGFACE_API_TOKEN}'}
70
- payload = {"inputs": story}
71
- response = requests.post(API_URL, headers=headers, json=payload)
72
- with open('story_speech.flac', 'wb') as file:
73
- file.write(response.content)
74
-
75
-
76
- # user interface
77
-
78
- def main():
79
- """
80
- Main function to run the Streamlit user interface for the Image-to-Story App.
81
- """
82
- st.set_page_config(page_title= "IMAGE TO STORY CONVERTER", page_icon= "🖼️")
83
- st.header("Image-to-Story Converter")
84
- #file uploader
85
- file_upload = st.file_uploader("Please choose a file to upload", type="jpg")
86
- #save file
87
- if file_upload is not None:
88
- try:
89
- image_bytes = file_upload.getvalue()
90
- with open(f'{file_upload.name}', "wb") as file:
91
- file.write(image_bytes)
92
- #display image
93
- st.image(file_upload, caption = "Uploaded image")
94
- #run functions
95
- file_name = file_upload.name
96
- text = image_to_text(f'{file_name}')
97
- if text:
98
- story = text_to_story(text)
99
- with st.expander('Generated image scenario'):
100
- st.write(text)
101
- if story:
102
- story_to_speech(story)
103
- with st.expander('Generated short story'):
104
- st.write(story)
105
- st.audio('story_speech.flac')
106
- else:
107
- st.error("Failed to generate a story from the text.")
108
- else:
109
- st.error("Failed to generate a text from the image.")
110
-
111
- except Exception as e:
112
- st.error(f"An error occurred: {e}")
113
- else:
114
- st.warning("Please upload an image to generate a story.")
115
-
116
-
117
- if __name__ == '__main__':
118
- main()