adi-123 commited on
Commit
f9533ad
·
verified ·
1 Parent(s): 8956bcd

Delete utils.py

Browse files
Files changed (1) hide show
  1. utils.py +0 -108
utils.py DELETED
@@ -1,108 +0,0 @@
1
- import re
2
- import os
3
- import streamlit as st
4
- from transformers import pipeline
5
- from typing import Dict, Optional
6
- from together import Together
7
- from gtts import gTTS
8
- from mail_sender import MailSender
9
-
10
-
11
- # Image-to-text
12
- def img2txt(url: str) -> str:
13
- print("Initializing captioning model...")
14
- captioning_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
15
-
16
- print("Generating text from the image...")
17
- text = captioning_model(url, max_new_tokens=20)[0]["generated_text"]
18
-
19
- print(text)
20
- return text
21
-
22
- # Text-to-story generation with LLM model
23
- def txt2story(prompt: str, top_k: int, top_p: float, temperature: float) -> str:
24
- # Load the Together API client
25
- client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
26
-
27
- # Modify the prompt based on user inputs and ensure a 250-word limit
28
- story_prompt = f"Write a short story of no more than 250 words based on the following prompt: {prompt}"
29
-
30
- # Call the LLM model
31
- stream = client.chat.completions.create(
32
- model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
33
- messages=[
34
- {"role": "system", "content": '''As an experienced short story writer, write a meaningful story influenced by the provided prompt.
35
- Ensure the story does not exceed 250 words.'''},
36
- {"role": "user", "content": story_prompt}
37
- ],
38
- top_k=top_k,
39
- top_p=top_p,
40
- temperature=temperature,
41
- stream=True
42
- )
43
-
44
- # Concatenate story chunks
45
- story = ''
46
- for chunk in stream:
47
- story += chunk.choices[0].delta.content
48
-
49
- return story
50
-
51
- # Text-to-speech
52
- def txt2speech(text: str) -> None:
53
- print("Converting text to speech using gTTS...")
54
-
55
- # Generate audio from the text
56
- tts = gTTS(text=text, lang='en')
57
-
58
- # Save the audio output to a file
59
- tts.save("audio_story.mp3")
60
-
61
- # Get user preferences for the story
62
- def get_user_preferences() -> Dict[str, str]:
63
- preferences = {
64
- 'continent': st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]),
65
- 'genre': st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]),
66
- 'setting': st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]),
67
- 'plot': st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]),
68
- 'tone': st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]),
69
- 'theme': st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]),
70
- 'conflict': st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]),
71
- 'twist': st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]),
72
- 'ending': st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
73
- }
74
- return preferences
75
-
76
-
77
- def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
78
- """
79
- Send the story text and audio file to the specified email address using python-mail-sender
80
- Returns True if successful, False otherwise
81
- """
82
- try:
83
- # Initialize the MailSender with env variables
84
- mail_sender = MailSender()
85
-
86
- # Email configuration
87
- subject = "Your Generated Story"
88
- content = f"Here's your generated story:\n\n{story_text}\n\nEnjoy!"
89
-
90
- # Send email
91
- mail_sender.send_mail(
92
- receiver_address=recipient_email,
93
- subject=subject,
94
- email_content=content,
95
- attached_files=[audio_file_path]
96
- )
97
- return True
98
-
99
- except Exception as e:
100
- print(f"Error sending email: {str(e)}")
101
- return False
102
-
103
- def validate_email(email: str) -> bool:
104
- """
105
- Basic email validation
106
- """
107
- pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
108
- return re.match(pattern, email) is not None