Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| from transformers import pipeline | |
| from typing import Dict | |
| from together import Together | |
| # Image-to-text | |
| def img2txt(url: str) -> str: | |
| print("Initializing captioning model...") | |
| captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
| print("Generating text from the image...") | |
| text = captioning_model(url, max_new_tokens=20)[0]["generated_text"] | |
| print(text) | |
| return text | |
| # Text-to-story generation with LLM model | |
| def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str: | |
| client = Together(api_key=os.environ.get("TOGETHER_API_KEY")) | |
| story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}" | |
| stream = client.chat.completions.create( | |
| model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", | |
| messages=[ | |
| {"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt. | |
| Ensure the story does not exceed 250 words.'''}, | |
| {"role": "user", "content": story_prompt} | |
| ], | |
| top_k=top_k, | |
| top_p=top_p, | |
| temperature=temperature, | |
| stream=True | |
| ) | |
| story = '' | |
| for chunk in stream: | |
| story += chunk.choices[0].delta.content | |
| return story | |
| # Text-to-speech | |
| def txt2speech(text: str) -> None: | |
| print("Initializing text-to-speech conversion...") | |
| API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits" | |
| headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"} | |
| payloads = {'inputs': text} | |
| response = requests.post(API_URL, headers=headers, json=payloads) | |
| with open('audio_story.mp3', 'wb') as file: | |
| file.write(response.content) | |
| # Get user preferences for the story | |
| def get_user_preferences(st) -> Dict[str, str]: | |
| preferences = {} | |
| preferences['continent'] = st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]) | |
| preferences['genre'] = st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]) | |
| preferences['setting'] = st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]) | |
| preferences['plot'] = st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]) | |
| preferences['tone'] = st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]) | |
| preferences['theme'] = st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]) | |
| preferences['conflict'] = st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]) | |
| preferences['twist'] = st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]) | |
| preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"]) | |
| return preferences |