Update app.py
Browse files
app.py
CHANGED
@@ -29,7 +29,6 @@ def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
|
|
29 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
30 |
messages=[
|
31 |
{"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
|
32 |
-
Ensure the story is full of positive inspiration & enthusiasm and concludes with a happy ending.
|
33 |
Ensure the story does not exceed 250 words.'''},
|
34 |
{"role": "user", "content": story_prompt}
|
35 |
],
|
@@ -58,22 +57,11 @@ def txt2speech(text: str) -> None:
|
|
58 |
with open('audio_story.mp3', 'wb') as file:
|
59 |
file.write(response.content)
|
60 |
|
61 |
-
# Story translation function
|
62 |
-
def translate_story(story: str, target_language: str) -> str:
|
63 |
-
# Translation pipeline
|
64 |
-
translation_model = pipeline("translation", model="SnypzZz/Llama2-13b-Language-translate")
|
65 |
-
|
66 |
-
print(f"Translating the story to {target_language}...")
|
67 |
-
translated_story = translation_model(story, max_length=400, tgt_lang=target_language)[0]['translation_text']
|
68 |
-
|
69 |
-
return translated_story
|
70 |
-
|
71 |
# Get user preferences for the story
|
72 |
def get_user_preferences() -> Dict[str, str]:
|
73 |
preferences = {}
|
74 |
|
75 |
preferences['continent'] = st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"])
|
76 |
-
preferences['language'] = st.selectbox("Language", ["English", "Hindi", "Spanish", "French", "German"])
|
77 |
preferences['genre'] = st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"])
|
78 |
preferences['setting'] = st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"])
|
79 |
preferences['plot'] = st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"])
|
@@ -82,7 +70,7 @@ def get_user_preferences() -> Dict[str, str]:
|
|
82 |
preferences['conflict'] = st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"])
|
83 |
preferences['magic_tech'] = st.selectbox("Magic/Technology", ["Advanced technology", "Magic system", "Supernatural abilities", "Alien technology"])
|
84 |
preferences['twist'] = st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"])
|
85 |
-
preferences['ending'] = st.selectbox("Ending", ["
|
86 |
|
87 |
return preferences
|
88 |
|
@@ -117,7 +105,7 @@ def main():
|
|
117 |
scenario = img2txt("uploaded_image.jpg") # Extracts text from the image
|
118 |
|
119 |
# Modify the prompt to include user preferences
|
120 |
-
prompt = f"Based on the image description: '{scenario}', create a {preferences['genre']} story set in {preferences['setting']}. " \
|
121 |
f"The story should have a {preferences['tone']} tone and explore the theme of {preferences['theme']}. " \
|
122 |
f"The main conflict should be {preferences['conflict']}. " \
|
123 |
f"Include {preferences['magic_tech']} as a key element. " \
|
@@ -125,10 +113,6 @@ def main():
|
|
125 |
|
126 |
story = txt2story(prompt, top_k, top_p, temperature) # Generates a story based on the image text, LLM params, and user preferences
|
127 |
|
128 |
-
# Translate the story if the user selected a non-English language
|
129 |
-
if preferences['language'] != "English":
|
130 |
-
story = translate_story(story, preferences['language'])
|
131 |
-
|
132 |
txt2speech(story) # Converts the story to audio
|
133 |
|
134 |
st.markdown("---")
|
|
|
29 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
30 |
messages=[
|
31 |
{"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
|
|
|
32 |
Ensure the story does not exceed 250 words.'''},
|
33 |
{"role": "user", "content": story_prompt}
|
34 |
],
|
|
|
57 |
with open('audio_story.mp3', 'wb') as file:
|
58 |
file.write(response.content)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# Get user preferences for the story
|
61 |
def get_user_preferences() -> Dict[str, str]:
|
62 |
preferences = {}
|
63 |
|
64 |
preferences['continent'] = st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"])
|
|
|
65 |
preferences['genre'] = st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"])
|
66 |
preferences['setting'] = st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"])
|
67 |
preferences['plot'] = st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"])
|
|
|
70 |
preferences['conflict'] = st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"])
|
71 |
preferences['magic_tech'] = st.selectbox("Magic/Technology", ["Advanced technology", "Magic system", "Supernatural abilities", "Alien technology"])
|
72 |
preferences['twist'] = st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"])
|
73 |
+
preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
|
74 |
|
75 |
return preferences
|
76 |
|
|
|
105 |
scenario = img2txt("uploaded_image.jpg") # Extracts text from the image
|
106 |
|
107 |
# Modify the prompt to include user preferences
|
108 |
+
prompt = f"Based on the image description: '{scenario}', create a {preferences['genre']} story set in {preferences['setting']} in {preferences['continent']}. " \
|
109 |
f"The story should have a {preferences['tone']} tone and explore the theme of {preferences['theme']}. " \
|
110 |
f"The main conflict should be {preferences['conflict']}. " \
|
111 |
f"Include {preferences['magic_tech']} as a key element. " \
|
|
|
113 |
|
114 |
story = txt2story(prompt, top_k, top_p, temperature) # Generates a story based on the image text, LLM params, and user preferences
|
115 |
|
|
|
|
|
|
|
|
|
116 |
txt2speech(story) # Converts the story to audio
|
117 |
|
118 |
st.markdown("---")
|