techasad commited on
Commit
aec954b
·
1 Parent(s): 4fe5145

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import pipeline
3
+ from langchain import PromptTemplate, LLMChain
4
+ from langchain.llms import GooglePalm
5
+
6
+ import requests
7
+ import os
8
+ import streamlit as st
9
+
10
+
11
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyD29fEos3V6S2L-AGSQgNu03GqZEIgJads"
12
+ os.environ ["HUGGINGFACEHUB_API_TOKEN"] = "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"
13
+
14
+ llm = GooglePalm(temperature = 0.7)
15
+
16
+
17
+ #image to text
18
+
19
+ def image2text(url):
20
+ image_to_text = pipeline("image-to-text", model = "Salesforce/blip-image-captioning-large")
21
+
22
+ text = image_to_text(
23
+ url)[0]['generated_text']
24
+
25
+ print(text)
26
+ return(text)
27
+
28
+ #story teller
29
+ def generate_story(scenario):
30
+ template = """"
31
+ You are a story teller;
32
+ you can generate a creative fun story based on a sample narrative, the story should not be more than 100 words;
33
+
34
+ CONTEXT: {scenario}
35
+ STORY:
36
+ """
37
+
38
+ prompt = PromptTemplate(template = template,
39
+ input_variables = ['scenario']
40
+ )
41
+ story_llm = LLMChain(llm=llm, prompt = prompt, verbose = True)
42
+
43
+ story = story_llm.predict(scenario = scenario)
44
+
45
+ print(story)
46
+ return(story)
47
+
48
+ #text to speech
49
+
50
+ def text2speech(message):
51
+ API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
52
+ headers = {"Authorization": "Bearer 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"}
53
+ payloads = {
54
+ "inputs":message
55
+ }
56
+ response = requests.post(API_URL, headers = headers, json= payloads)
57
+ with open("audio.flac", "wb") as file:
58
+ file.write(response.content)
59
+
60
+
61
+
62
+ def main():
63
+ st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
64
+ st.header("Turn Your Image to Audio Story")
65
+ uploaded_file = st.file_uploader("Select an Image...")
66
+
67
+ if uploaded_file is not None:
68
+ print(uploaded_file)
69
+ bytes_data = uploaded_file.getvalue()
70
+ with open(uploaded_file.name, 'wb') as file:
71
+ file.write(bytes_data)
72
+ st.image(uploaded_file, caption="Uploaded Image",
73
+ use_column_width= True)
74
+
75
+ scenario = image2text(uploaded_file.name)
76
+ st.subheader("Image Details:")
77
+ st.write(scenario)
78
+
79
+ story = generate_story(scenario)
80
+ st.subheader("Story:")
81
+ st.write(story)
82
+
83
+ text2speech(story)
84
+ st.subheader("Generated Audio:")
85
+ st.audio("audio.flac", format="audio/flac")
86
+
87
+ # Add a download link for the audio
88
+ st.subheader("Download Audio:")
89
+ with open("audio.flac", "rb") as audio_file:
90
+ st.download_button(label="Download Audio", data=audio_file, file_name="generated_audio.flac", mime="audio/flac")
91
+
92
+
93
+ if __name__ == "__main__":
94
+ main()