adi-123 commited on
Commit
8738846
·
verified ·
1 Parent(s): 8ab2cd0

Create utils.py

Browse files

writing modularized code

Files changed (1) hide show
  1. utils.py +67 -0
utils.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Image-to-text
2
+ def img2txt(url: str) -> str:
3
+ print("Initializing captioning model...")
4
+ captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
5
+
6
+ print("Generating text from the image...")
7
+ text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
8
+
9
+ print(text)
10
+ return text
11
+
12
+ # Text-to-story generation with LLM model
13
+ def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
14
+ # Load the Together API client
15
+ client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
16
+
17
+ # Modify the prompt based on user inputs and ensure a 250-word limit
18
+ story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}"
19
+
20
+ # Call the LLM model
21
+ stream = client.chat.completions.create(
22
+ model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
23
+ messages=[
24
+ {"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
25
+ Ensure the story does not exceed 250 words.'''},
26
+ {"role": "user", "content": story_prompt}
27
+ ],
28
+ top_k=top_k,
29
+ top_p=top_p,
30
+ temperature=temperature,
31
+ stream=True
32
+ )
33
+
34
+ # Concatenate story chunks
35
+ story = ''
36
+ for chunk in stream:
37
+ story += chunk.choices[0].delta.content
38
+
39
+ return story
40
+
41
+ # Text-to-speech
42
+ def txt2speech(text: str) -> None:
43
+ print("Initializing text-to-speech conversion...")
44
+ API_URL = "https://api-inference.huggingface.co/models/espnet/kan-bayashi_ljspeech_vits"
45
+ headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"}
46
+ payloads = {'inputs': text}
47
+
48
+ response = requests.post(API_URL, headers=headers, json=payloads)
49
+
50
+ with open('audio_story.mp3', 'wb') as file:
51
+ file.write(response.content)
52
+
53
+ # Get user preferences for the story
54
+ def get_user_preferences() -> Dict[str, str]:
55
+ preferences = {}
56
+
57
+ preferences['continent'] = st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"])
58
+ preferences['genre'] = st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"])
59
+ preferences['setting'] = st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"])
60
+ preferences['plot'] = st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"])
61
+ preferences['tone'] = st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"])
62
+ preferences['theme'] = st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"])
63
+ preferences['conflict'] = st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"])
64
+ preferences['twist'] = st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"])
65
+ preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
66
+
67
+ return preferences