adi-123 commited on
Commit
b9f996a
·
verified ·
1 Parent(s): 58f448b

Create utils.py

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