BSrikanth commited on
Commit
eb2392a
·
1 Parent(s): 2e91fd5

Upload 2 files

Browse files
Files changed (2) hide show
  1. .env +4 -0
  2. app.py +70 -0
.env ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ HUGGINGPHASE_API_TOEKEN = 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
3
+
4
+ OPENAI_API_KEY=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
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import find_dotenv, load_dotenv
2
+ from transformers import pipeline
3
+ from langchain import PromptTemplate, LLMChain, OpenAI
4
+ import requests
5
+ import os
6
+ import streamlit as st
7
+
8
+ load_dotenv(find_dotenv())
9
+ HUGGINGPHASE_API_TOKEN = os.getenv("HUGGINGPHASE_API_TOKEN")
10
+
11
+ # Image2Text
12
+
13
+ def img2text(url):
14
+ image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
15
+ text = img2text(url)[0]["generated_text"]
16
+ print(text)
17
+ return text
18
+
19
+ # LLM
20
+
21
+ def generate_story(scenario):
22
+ template = """
23
+ you are a story teller
24
+ you can generate a short story based on simple narrative, the story should be more than 20 words;
25
+ CONTEXT:{scenario},
26
+ STORY:
27
+ """
28
+ prompt = PromptTemplate(template=template, input_variable=["scenario"])
29
+ story_llm = LLMChain(llm=OpenAI(model_name="gpt-3.5-turbo", temperature=1), prompt=prompt, verbose=True)
30
+ story = story_llm.predict(scenario=scenario)
31
+ print(story)
32
+ return story
33
+
34
+ # Text to Speech
35
+
36
+ def text2speech(message):
37
+ API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
38
+ headers = {"Authorization": f"Bearer {HUGGINGPHASE_API_TOKEN}"}
39
+ payloads = {
40
+ "inputs": message
41
+ }
42
+
43
+ response = requests.post(API_URL, headers=headers, json=payloads)
44
+
45
+ with open('audio.flac', 'wb') as file:
46
+ file.write(response.content)
47
+
48
+ def main():
49
+ st.set_page_config(page_title="img 2 Audio story", page_icon='🤖')
50
+ st.header("Turn img into an audio story")
51
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
52
+
53
+ if uploaded_file is not None:
54
+ print(uploaded_file)
55
+ with open(uploaded_file.name, "wb") as file:
56
+ file.write(uploaded_file.getvalue())
57
+ st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
58
+ scenario = img2text(uploaded_file.name)
59
+ story = generate_story(scenario)
60
+ text2speech(story)
61
+
62
+ with st.expander("Scenario"):
63
+ st.write(scenario)
64
+ with st.expander("Story"):
65
+ st.write(story)
66
+
67
+ st.audio("audio.flac")
68
+
69
+ if __name__ == '__main__':
70
+ main()