Spaces:
Sleeping
Sleeping
File size: 9,506 Bytes
4d5de4a |
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
import io
import base64
import streamlit as st
import os
from together import Together
from PIL import Image
import requests
from io import BytesIO
import json
from streamlit_lottie import st_lottie
from streamlit_option_menu import option_menu
import time
# Set page config
st.set_page_config(
page_title="TeleGuide | AI Telecom Assistant",
page_icon="π°οΈ",
layout="wide",
initial_sidebar_state="expanded"
)
# Load Lottie animation
def load_lottie(url: str):
try:
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
except:
return None
# Custom CSS with animations and improved styling
st.markdown("""
<style>
/* Main app styling */
.stApp {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
/* Card-like containers */
.css-1r6slb0 {
background: white;
border-radius: 20px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Header styling */
.main-header {
font-size: 2.5rem;
font-weight: 700;
color: #1E3D59;
text-align: center;
margin-bottom: 2rem;
animation: fadeIn 1.5s ease-in;
}
/* Button styling */
.stButton>button {
width: 100%;
border-radius: 10px;
background: linear-gradient(45deg, #2193b0, #6dd5ed);
color: white;
border: none;
padding: 0.5rem 1rem;
transition: all 0.3s ease;
}
.stButton>button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
/* Input field styling */
.stTextInput>div>div>input,
.stTextArea>div>div>textarea {
border-radius: 10px;
border: 2px solid #e0e0e0;
padding: 10px;
background-color: white;
}
/* Sidebar styling */
.css-1d391kg {
background: linear-gradient(180deg, #1E3D59 0%, #2193b0 100%);
}
/* Animations */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.fade-in {
animation: fadeIn 1s ease-in;
}
/* Status messages */
.success-message {
padding: 1rem;
border-radius: 10px;
background-color: #d4edda;
color: #155724;
margin: 1rem 0;
}
.error-message {
padding: 1rem;
border-radius: 10px;
background-color: #f8d7da;
color: #721c24;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Initialize the Together client with error handling
@st.cache_resource
def get_together_client():
try:
return Together(api_key=st.secrets["api_key"])
except Exception as e:
st.error(f"Error initializing API client: {str(e)}")
return None
client = get_together_client()
# Load animations
lottie_telecom = load_lottie("https://assets4.lottiefiles.com/packages/lf20_qz3tpn4w.json")
lottie_analysis = load_lottie("https://assets4.lottiefiles.com/packages/lf20_xh83pj1k.json")
def process_text_query(query, model="meta-llama/Llama-3.2-3B-Instruct-Turbo"):
try:
with st.spinner("Processing your query..."):
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are TeleGuide, an expert AI assistant specialized in telecommunication tasks. Provide detailed, practical, and accurate information."},
{"role": "user", "content": query}
],
max_tokens=500,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
st.error(f"Error processing query: {str(e)}")
return None
def process_image_query(image_base64, query, model="meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo"):
try:
with st.spinner("Analyzing image..."):
system_message = "You are TeleGuide, an expert AI assistant in telecommunications infrastructure analysis."
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_message},
{
"role": "user",
"content": [
{"type": "text", "text": query},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
]
}
],
max_tokens=500,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
st.error(f"Error analyzing image: {str(e)}")
return None
def image_to_base64(image):
try:
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')
except Exception as e:
st.error(f"Error converting image: {str(e)}")
return None
# Sidebar
with st.sidebar:
st.title("π°οΈ TeleGuide")
st_lottie(lottie_telecom, height=200)
st.markdown("---")
st.info("Your AI-powered telecommunication assistant, providing expert analysis and insights.")
st.markdown("### Features")
st.markdown("""
- π Text Analysis
- π Document Processing
- πΌοΈ Image Analysis
- π‘ Infrastructure Planning
""")
st.markdown("---")
st.markdown("#### Powered by Advanced AI")
st.caption("Using Llama 3.2 Models")
# Main content
st.markdown('<h1 class="main-header">Welcome to TeleGuide</h1>', unsafe_allow_html=True)
# Navigation
selected = option_menu(
menu_title=None,
options=["Text Analysis", "Document Processing", "Image Analysis"],
icons=["chat-dots", "file-text", "image"],
menu_icon="cast",
default_index=0,
orientation="horizontal",
styles={
"container": {"padding": "0!important", "background-color": "transparent"},
"icon": {"color": "#1E3D59", "font-size": "25px"},
"nav-link": {
"font-size": "20px",
"text-align": "center",
"margin": "0px",
"--hover-color": "#eee",
},
"nav-link-selected": {"background-color": "#2193b0", "color": "white"},
}
)
if selected == "Text Analysis":
st.markdown("### π¬ Text Analysis")
st_lottie(lottie_analysis, height=200)
query = st.text_area("Enter your telecommunications query:", height=100)
if st.button("Process Query", key="text_query"):
if query:
response = process_text_query(query)
if response:
st.markdown('<div class="success-message">β
Query processed successfully!</div>', unsafe_allow_html=True)
st.markdown("### Response:")
st.write(response)
else:
st.warning("Please enter a query.")
elif selected == "Document Processing":
st.markdown("### π Document Analysis")
document_type = st.selectbox(
"Select Document Type",
["Regulatory Document", "Technical Specification", "Network Planning", "Customer Inquiry"]
)
text_input = st.text_area("Enter document text:", height=150)
if st.button("Analyze Document"):
if text_input:
analysis_prompt = f"Analyze the following {document_type.lower()}: {text_input}"
response = process_text_query(analysis_prompt)
if response:
st.markdown('<div class="success-message">β
Document analyzed successfully!</div>', unsafe_allow_html=True)
st.markdown("### Analysis Results:")
st.write(response)
else:
st.warning("Please enter document text.")
elif selected == "Image Analysis":
st.markdown("### πΌοΈ Image Analysis")
image_type = st.selectbox(
"Select Image Type",
["Network Infrastructure", "Equipment", "Site Survey", "Signal Coverage"]
)
uploaded_file = st.file_uploader("Upload image...", type=["jpg", "png", "jpeg"])
if uploaded_file:
try:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
query = st.text_area("Analysis requirements:", height=100)
if st.button("Analyze Image"):
if query:
image_base64 = image_to_base64(image)
if image_base64:
response = process_image_query(image_base64, query)
if response:
st.markdown('<div class="success-message">β
Image analyzed successfully!</div>', unsafe_allow_html=True)
st.markdown("### Analysis Results:")
st.write(response)
else:
st.warning("Please enter analysis requirements.")
except Exception as e:
st.error(f"Error processing image: {str(e)}")
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: #666;'>
<p>TeleGuide - Your AI-Powered Telecommunications Assistant</p>
<p>Made with β€οΈ for telecom professionals</p>
</div>
""",
unsafe_allow_html=True
) |