Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,33 @@ import streamlit as st
|
|
| 3 |
from textwrap import dedent
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Tool import
|
| 7 |
from crewai.tools.gemini_tools import GeminiSearchTools
|
| 8 |
|
|
@@ -16,7 +43,64 @@ from crewai import Agent, Task, Crew, Process
|
|
| 16 |
# Retrieve API Key from Environment Variable
|
| 17 |
GOOGLE_AI_STUDIO = os.environ.get('GOOGLE_API_KEY')
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Ensure the API key is available
|
| 22 |
if not GOOGLE_AI_STUDIO:
|
|
@@ -27,7 +111,7 @@ else:
|
|
| 27 |
|
| 28 |
# Base Example with Gemini Search
|
| 29 |
|
| 30 |
-
TITLE1 = """<h1 align="center">Clarifai
|
| 31 |
|
| 32 |
def crewai_process(research_topic):
|
| 33 |
# Define your agents with roles and goals
|
|
@@ -200,7 +284,7 @@ def crewai_process(research_topic):
|
|
| 200 |
import streamlit as st
|
| 201 |
|
| 202 |
# Create tabs
|
| 203 |
-
tab1, tab2, tab3 = st.tabs(["Create Your Story", "
|
| 204 |
|
| 205 |
# Tab 1: Introduction
|
| 206 |
with tab1:
|
|
@@ -220,8 +304,34 @@ with tab1:
|
|
| 220 |
|
| 221 |
# Tab 2: Data Visualization
|
| 222 |
with tab2:
|
| 223 |
-
|
| 224 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# Tab 3: User Input and Results
|
| 227 |
with tab3:
|
|
|
|
| 3 |
from textwrap import dedent
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
|
| 7 |
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
|
| 8 |
+
from clarifai_grpc.grpc.api.status.status_code_pb2 import SUCCESS
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
from nltk.tokenize import sent_tokenize
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
# Ensure nltk punkt tokenizer data is downloaded
|
| 15 |
+
import nltk
|
| 16 |
+
nltk.download('punkt')
|
| 17 |
+
|
| 18 |
+
# Constants for image and audio generation
|
| 19 |
+
USER_ID_IMG = 'openai'
|
| 20 |
+
APP_ID_IMG = 'dall-e'
|
| 21 |
+
MODEL_ID_IMG = 'dall-e-3'
|
| 22 |
+
MODEL_VERSION_ID_IMG = 'dc9dcb6ee67543cebc0b9a025861b868'
|
| 23 |
+
|
| 24 |
+
USER_ID_AUDIO = 'eleven-labs'
|
| 25 |
+
APP_ID_AUDIO = 'audio-generation'
|
| 26 |
+
MODEL_ID_AUDIO = 'speech-synthesis'
|
| 27 |
+
MODEL_VERSION_ID_AUDIO = 'f2cead3a965f4c419a61a4a9b501095c'
|
| 28 |
+
|
| 29 |
+
# Retrieve PAT from environment variable
|
| 30 |
+
PAT = os.getenv('CLARIFAI_PAT')
|
| 31 |
+
|
| 32 |
+
|
| 33 |
# Tool import
|
| 34 |
from crewai.tools.gemini_tools import GeminiSearchTools
|
| 35 |
|
|
|
|
| 43 |
# Retrieve API Key from Environment Variable
|
| 44 |
GOOGLE_AI_STUDIO = os.environ.get('GOOGLE_API_KEY')
|
| 45 |
|
| 46 |
+
# Story book
|
| 47 |
+
|
| 48 |
+
# Function to generate image using Clarifai
|
| 49 |
+
def generate_image(prompt):
|
| 50 |
+
channel = ClarifaiChannel.get_grpc_channel()
|
| 51 |
+
stub = service_pb2_grpc.V2Stub(channel)
|
| 52 |
+
metadata = (('authorization', 'Key ' + PAT),)
|
| 53 |
+
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID_IMG, app_id=APP_ID_IMG)
|
| 54 |
+
|
| 55 |
+
post_model_outputs_response = stub.PostModelOutputs(
|
| 56 |
+
service_pb2.PostModelOutputsRequest(
|
| 57 |
+
user_app_id=userDataObject,
|
| 58 |
+
model_id=MODEL_ID_IMG,
|
| 59 |
+
version_id=MODEL_VERSION_ID_IMG,
|
| 60 |
+
inputs=[resources_pb2.Input(data=resources_pb2.Data(text=resources_pb2.Text(raw=prompt)))]
|
| 61 |
+
),
|
| 62 |
+
metadata=metadata
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if post_model_outputs_response.status.code != SUCCESS:
|
| 66 |
+
return None, "Error in generating image: " + post_model_outputs_response.status.description
|
| 67 |
+
else:
|
| 68 |
+
output = post_model_outputs_response.outputs[0].data.image.base64
|
| 69 |
+
image = Image.open(BytesIO(output))
|
| 70 |
+
return image, None
|
| 71 |
+
|
| 72 |
+
# Function to generate audio using Clarifai
|
| 73 |
+
def generate_audio(prompt):
|
| 74 |
+
channel = ClarifaiChannel.get_grpc_channel()
|
| 75 |
+
stub = service_pb2_grpc.V2Stub(channel)
|
| 76 |
+
metadata = (('authorization', 'Key ' + PAT),)
|
| 77 |
+
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID_AUDIO, app_id=APP_ID_AUDIO)
|
| 78 |
+
|
| 79 |
+
response = stub.PostModelOutputs(
|
| 80 |
+
service_pb2.PostModelOutputsRequest(
|
| 81 |
+
user_app_id=userDataObject,
|
| 82 |
+
model_id=MODEL_ID_AUDIO,
|
| 83 |
+
version_id=MODEL_VERSION_ID_AUDIO,
|
| 84 |
+
inputs=[resources_pb2.Input(data=resources_pb2.Data(text=resources_pb2.Text(raw=prompt)))]
|
| 85 |
+
),
|
| 86 |
+
metadata=metadata
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
if response.status.code != SUCCESS:
|
| 90 |
+
return None, "Error in generating audio: " + response.status.description
|
| 91 |
+
else:
|
| 92 |
+
audio_output = response.outputs[0].data.audio.base64
|
| 93 |
+
return audio_output, None
|
| 94 |
+
|
| 95 |
+
# Function to split text into sentences and then chunk them
|
| 96 |
+
|
| 97 |
+
def split_text_into_sentences_and_chunks(text, n=8):
|
| 98 |
+
sentences = sent_tokenize(text)
|
| 99 |
+
total_sentences = len(sentences)
|
| 100 |
+
sentences_per_chunk = max(2, total_sentences // n)
|
| 101 |
+
return [sentences[i:i + sentences_per_chunk] for i in range(0, total_sentences, sentences_per_chunk)]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
|
| 105 |
# Ensure the API key is available
|
| 106 |
if not GOOGLE_AI_STUDIO:
|
|
|
|
| 111 |
|
| 112 |
# Base Example with Gemini Search
|
| 113 |
|
| 114 |
+
TITLE1 = """<h1 align="center">Clarifai NextGen Hackathon</h1>"""
|
| 115 |
|
| 116 |
def crewai_process(research_topic):
|
| 117 |
# Define your agents with roles and goals
|
|
|
|
| 284 |
import streamlit as st
|
| 285 |
|
| 286 |
# Create tabs
|
| 287 |
+
tab1, tab2, tab3 = st.tabs(["Create Your Story Script", "Build Your Image/Audio Book", "Interact with Your Charaters"])
|
| 288 |
|
| 289 |
# Tab 1: Introduction
|
| 290 |
with tab1:
|
|
|
|
| 304 |
|
| 305 |
# Tab 2: Data Visualization
|
| 306 |
with tab2:
|
| 307 |
+
# Streamlit page configuration
|
| 308 |
+
st.set_page_config(page_title='DALL-E Image and Audio Generator', layout='wide')
|
| 309 |
+
|
| 310 |
+
# Streamlit sidebar elements
|
| 311 |
+
st.sidebar.title("DALL-E Image and Audio Generator")
|
| 312 |
+
text_block = st.sidebar.text_area("Enter your text block:")
|
| 313 |
+
|
| 314 |
+
# Streamlit main page
|
| 315 |
+
st.title('Generate Images and Audio with DALL-E and Clarifai')
|
| 316 |
+
if st.sidebar.button("Generate Images and Audio"):
|
| 317 |
+
sentence_chunks = split_text_into_sentences_and_chunks(text_block, 8)
|
| 318 |
+
prompts = [' '.join(chunk) for chunk in sentence_chunks]
|
| 319 |
+
cols = st.columns(4)
|
| 320 |
+
with st.spinner('Generating Content...'):
|
| 321 |
+
for i, prompt in enumerate(prompts):
|
| 322 |
+
image, img_error = generate_image(prompt)
|
| 323 |
+
audio, audio_error = generate_audio(prompt)
|
| 324 |
+
|
| 325 |
+
with cols[i % 4]:
|
| 326 |
+
if img_error:
|
| 327 |
+
st.error(img_error)
|
| 328 |
+
else:
|
| 329 |
+
st.image(image, prompt, use_column_width=True)
|
| 330 |
+
|
| 331 |
+
if audio_error:
|
| 332 |
+
st.error(audio_error)
|
| 333 |
+
else:
|
| 334 |
+
st.audio(audio, format='audio/wav')
|
| 335 |
|
| 336 |
# Tab 3: User Input and Results
|
| 337 |
with tab3:
|