File size: 4,579 Bytes
f3ad791
 
 
 
 
 
27e9a0a
f3ad791
27e9a0a
5ab00b0
 
f3ad791
 
 
 
27e9a0a
 
 
f3ad791
 
 
 
 
 
 
 
 
27e9a0a
f3ad791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27e9a0a
 
 
c3f366c
 
27e9a0a
 
 
 
 
 
 
c3f366c
 
 
 
27e9a0a
 
c3f366c
27e9a0a
 
 
f3ad791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27e9a0a
 
 
 
 
2fdfd07
27e9a0a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import streamlit as st
from together import Together
import base64
from PIL import Image
from io import BytesIO
import os
from elevenlabs.client import ElevenLabs

# Set API keys as environment variables
os.environ["TOGETHER_API_KEY"] = st.secrets['together_api']
os.environ["ELEVENLABS_API_KEY"] = st.secrets['elevenlabs_api']

# Initialize the Together client
together_client = Together(api_key=os.environ["TOGETHER_API_KEY"])

# Initialize ElevenLabs client
elevenlabs_client = ElevenLabs(api_key=os.environ["ELEVENLABS_API_KEY"])

# Function to encode image to base64
def encode_image(image):
    buffered = BytesIO()
    image.save(buffered, format="JPEG")
    image_bytes = buffered.getvalue()
    return base64.b64encode(image_bytes).decode('utf-8')

# Function to get image description from Together API
def get_image_description(image):
    get_description_prompt = "Describe the given image in detail in only 20 words max."
    
    # Encode the image to base64
    base64_image = encode_image(image)

    # Create the request to Together API
    response = together_client.chat.completions.create(
        model="meta-llama/Llama-Vision-Free",
        messages=[{
            "role": "user",
            "content": [
                {"type": "text", "text": get_description_prompt},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}",
                    },
                },
            ],
        }],
        stream=False,
    )

    # Return the result from the API
    return response.choices[0].message.content

# Function to convert text to speech using ElevenLabs
def tts(text):
    try:
        # Generate the audio (returns a generator)
        audio_generator = elevenlabs_client.text_to_speech.convert(
            text=text,
            voice_id="JBFqnCBsd6RMkjVDRZzb",  # Replace with your preferred voice ID
            model_id="eleven_multilingual_v2",
            output_format="mp3_44100_128",
        )
        
        # Save the audio to a temporary file
        audio_file_path = "temp_audio.mp3"
        with open(audio_file_path, "wb") as f:
            for chunk in audio_generator:
                f.write(chunk)
        
        # Play the audio in Streamlit
        st.audio(audio_file_path, format="audio/mp3")
    except Exception as e:
        st.error(f"Error generating speech: {e}")

# Custom CSS for a futuristic look
st.markdown(
    """
    <style>
    .stApp {
        background: linear-gradient(135deg, #1e1e2f, #2a2a40);
        color: #ffffff;
        font-family: 'Arial', sans-serif;
    }
    .stButton>button {
        background: linear-gradient(135deg, #6a11cb, #2575fc);
        color: white;
        border: none;
        border-radius: 12px;
        padding: 10px 20px;
        font-size: 16px;
        font-weight: bold;
    }
    .stButton>button:hover {
        background: linear-gradient(135deg, #2575fc, #6a11cb);
    }
    .stImage {
        border-radius: 12px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    }
    .stMarkdown h1 {
        color: #6a11cb;
        text-align: center;
        font-size: 36px;
        font-weight: bold;
    }
    .stMarkdown h2 {
        color: #2575fc;
        font-size: 24px;
        font-weight: bold;
    }
    .stSpinner>div {
        color: #6a11cb;
    }
    </style>
    """,
    unsafe_allow_html=True,
)

# Streamlit app layout
st.title("๐Ÿ”ฎ Visox | Koshur AI")
st.markdown("### See the world through AI's eyes!")

# Sidebar for additional info
st.sidebar.markdown("## About")
st.sidebar.markdown("This app uses advanced AI to describe what it sees through your camera in real-time.")
st.sidebar.markdown("Powered by [Together AI](https://together.ai) and Streamlit.")

# Access the camera
img_file_buffer = st.camera_input("Take a picture")

if img_file_buffer is not None:
    try:
        # Convert the image file buffer to a PIL Image
        img = Image.open(img_file_buffer)

        # Display the captured image
        st.image(img, caption='Captured Image', width=300)

        # Get and display the description
        with st.spinner('๐Ÿ” Analyzing the image...'):
            description = get_image_description(img)
        st.success('โœ… Analysis complete!')
        st.markdown("### AI Description:")
        st.write(description)

        # Convert description to speech and play it
        if st.button("๐Ÿ”Š Read Aloud"):
            tts(description)
    except Exception as e:
        st.error(f"An error occurred: {e}")