ibrahim313 commited on
Commit
4d5de4a
Β·
verified Β·
1 Parent(s): d469e03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +295 -0
app.py ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import base64
3
+ import streamlit as st
4
+ import os
5
+ from together import Together
6
+ from PIL import Image
7
+ import requests
8
+ from io import BytesIO
9
+ import json
10
+ from streamlit_lottie import st_lottie
11
+ from streamlit_option_menu import option_menu
12
+ import time
13
+
14
+ # Set page config
15
+ st.set_page_config(
16
+ page_title="TeleGuide | AI Telecom Assistant",
17
+ page_icon="πŸ›°οΈ",
18
+ layout="wide",
19
+ initial_sidebar_state="expanded"
20
+ )
21
+
22
+ # Load Lottie animation
23
+ def load_lottie(url: str):
24
+ try:
25
+ r = requests.get(url)
26
+ if r.status_code != 200:
27
+ return None
28
+ return r.json()
29
+ except:
30
+ return None
31
+
32
+ # Custom CSS with animations and improved styling
33
+ st.markdown("""
34
+ <style>
35
+ /* Main app styling */
36
+ .stApp {
37
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
38
+ }
39
+
40
+ /* Card-like containers */
41
+ .css-1r6slb0 {
42
+ background: white;
43
+ border-radius: 20px;
44
+ padding: 20px;
45
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
46
+ }
47
+
48
+ /* Header styling */
49
+ .main-header {
50
+ font-size: 2.5rem;
51
+ font-weight: 700;
52
+ color: #1E3D59;
53
+ text-align: center;
54
+ margin-bottom: 2rem;
55
+ animation: fadeIn 1.5s ease-in;
56
+ }
57
+
58
+ /* Button styling */
59
+ .stButton>button {
60
+ width: 100%;
61
+ border-radius: 10px;
62
+ background: linear-gradient(45deg, #2193b0, #6dd5ed);
63
+ color: white;
64
+ border: none;
65
+ padding: 0.5rem 1rem;
66
+ transition: all 0.3s ease;
67
+ }
68
+
69
+ .stButton>button:hover {
70
+ transform: translateY(-2px);
71
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
72
+ }
73
+
74
+ /* Input field styling */
75
+ .stTextInput>div>div>input,
76
+ .stTextArea>div>div>textarea {
77
+ border-radius: 10px;
78
+ border: 2px solid #e0e0e0;
79
+ padding: 10px;
80
+ background-color: white;
81
+ }
82
+
83
+ /* Sidebar styling */
84
+ .css-1d391kg {
85
+ background: linear-gradient(180deg, #1E3D59 0%, #2193b0 100%);
86
+ }
87
+
88
+ /* Animations */
89
+ @keyframes fadeIn {
90
+ from { opacity: 0; transform: translateY(20px); }
91
+ to { opacity: 1; transform: translateY(0); }
92
+ }
93
+
94
+ .fade-in {
95
+ animation: fadeIn 1s ease-in;
96
+ }
97
+
98
+ /* Status messages */
99
+ .success-message {
100
+ padding: 1rem;
101
+ border-radius: 10px;
102
+ background-color: #d4edda;
103
+ color: #155724;
104
+ margin: 1rem 0;
105
+ }
106
+
107
+ .error-message {
108
+ padding: 1rem;
109
+ border-radius: 10px;
110
+ background-color: #f8d7da;
111
+ color: #721c24;
112
+ margin: 1rem 0;
113
+ }
114
+ </style>
115
+ """, unsafe_allow_html=True)
116
+
117
+ # Initialize the Together client with error handling
118
+ @st.cache_resource
119
+ def get_together_client():
120
+ try:
121
+ return Together(api_key=st.secrets["api_key"])
122
+ except Exception as e:
123
+ st.error(f"Error initializing API client: {str(e)}")
124
+ return None
125
+
126
+ client = get_together_client()
127
+
128
+ # Load animations
129
+ lottie_telecom = load_lottie("https://assets4.lottiefiles.com/packages/lf20_qz3tpn4w.json")
130
+ lottie_analysis = load_lottie("https://assets4.lottiefiles.com/packages/lf20_xh83pj1k.json")
131
+
132
+ def process_text_query(query, model="meta-llama/Llama-3.2-3B-Instruct-Turbo"):
133
+ try:
134
+ with st.spinner("Processing your query..."):
135
+ response = client.chat.completions.create(
136
+ model=model,
137
+ messages=[
138
+ {"role": "system", "content": "You are TeleGuide, an expert AI assistant specialized in telecommunication tasks. Provide detailed, practical, and accurate information."},
139
+ {"role": "user", "content": query}
140
+ ],
141
+ max_tokens=500,
142
+ temperature=0.7
143
+ )
144
+ return response.choices[0].message.content
145
+ except Exception as e:
146
+ st.error(f"Error processing query: {str(e)}")
147
+ return None
148
+
149
+ def process_image_query(image_base64, query, model="meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo"):
150
+ try:
151
+ with st.spinner("Analyzing image..."):
152
+ system_message = "You are TeleGuide, an expert AI assistant in telecommunications infrastructure analysis."
153
+ response = client.chat.completions.create(
154
+ model=model,
155
+ messages=[
156
+ {"role": "system", "content": system_message},
157
+ {
158
+ "role": "user",
159
+ "content": [
160
+ {"type": "text", "text": query},
161
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
162
+ ]
163
+ }
164
+ ],
165
+ max_tokens=500,
166
+ temperature=0.7
167
+ )
168
+ return response.choices[0].message.content
169
+ except Exception as e:
170
+ st.error(f"Error analyzing image: {str(e)}")
171
+ return None
172
+
173
+ def image_to_base64(image):
174
+ try:
175
+ buffered = io.BytesIO()
176
+ image.save(buffered, format="JPEG")
177
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
178
+ except Exception as e:
179
+ st.error(f"Error converting image: {str(e)}")
180
+ return None
181
+
182
+ # Sidebar
183
+ with st.sidebar:
184
+ st.title("πŸ›°οΈ TeleGuide")
185
+ st_lottie(lottie_telecom, height=200)
186
+ st.markdown("---")
187
+ st.info("Your AI-powered telecommunication assistant, providing expert analysis and insights.")
188
+ st.markdown("### Features")
189
+ st.markdown("""
190
+ - πŸ“ Text Analysis
191
+ - πŸ“„ Document Processing
192
+ - πŸ–ΌοΈ Image Analysis
193
+ - πŸ“‘ Infrastructure Planning
194
+ """)
195
+ st.markdown("---")
196
+ st.markdown("#### Powered by Advanced AI")
197
+ st.caption("Using Llama 3.2 Models")
198
+
199
+ # Main content
200
+ st.markdown('<h1 class="main-header">Welcome to TeleGuide</h1>', unsafe_allow_html=True)
201
+
202
+ # Navigation
203
+ selected = option_menu(
204
+ menu_title=None,
205
+ options=["Text Analysis", "Document Processing", "Image Analysis"],
206
+ icons=["chat-dots", "file-text", "image"],
207
+ menu_icon="cast",
208
+ default_index=0,
209
+ orientation="horizontal",
210
+ styles={
211
+ "container": {"padding": "0!important", "background-color": "transparent"},
212
+ "icon": {"color": "#1E3D59", "font-size": "25px"},
213
+ "nav-link": {
214
+ "font-size": "20px",
215
+ "text-align": "center",
216
+ "margin": "0px",
217
+ "--hover-color": "#eee",
218
+ },
219
+ "nav-link-selected": {"background-color": "#2193b0", "color": "white"},
220
+ }
221
+ )
222
+
223
+ if selected == "Text Analysis":
224
+ st.markdown("### πŸ’¬ Text Analysis")
225
+ st_lottie(lottie_analysis, height=200)
226
+
227
+ query = st.text_area("Enter your telecommunications query:", height=100)
228
+ if st.button("Process Query", key="text_query"):
229
+ if query:
230
+ response = process_text_query(query)
231
+ if response:
232
+ st.markdown('<div class="success-message">βœ… Query processed successfully!</div>', unsafe_allow_html=True)
233
+ st.markdown("### Response:")
234
+ st.write(response)
235
+ else:
236
+ st.warning("Please enter a query.")
237
+
238
+ elif selected == "Document Processing":
239
+ st.markdown("### πŸ“„ Document Analysis")
240
+ document_type = st.selectbox(
241
+ "Select Document Type",
242
+ ["Regulatory Document", "Technical Specification", "Network Planning", "Customer Inquiry"]
243
+ )
244
+
245
+ text_input = st.text_area("Enter document text:", height=150)
246
+ if st.button("Analyze Document"):
247
+ if text_input:
248
+ analysis_prompt = f"Analyze the following {document_type.lower()}: {text_input}"
249
+ response = process_text_query(analysis_prompt)
250
+ if response:
251
+ st.markdown('<div class="success-message">βœ… Document analyzed successfully!</div>', unsafe_allow_html=True)
252
+ st.markdown("### Analysis Results:")
253
+ st.write(response)
254
+ else:
255
+ st.warning("Please enter document text.")
256
+
257
+ elif selected == "Image Analysis":
258
+ st.markdown("### πŸ–ΌοΈ Image Analysis")
259
+ image_type = st.selectbox(
260
+ "Select Image Type",
261
+ ["Network Infrastructure", "Equipment", "Site Survey", "Signal Coverage"]
262
+ )
263
+
264
+ uploaded_file = st.file_uploader("Upload image...", type=["jpg", "png", "jpeg"])
265
+ if uploaded_file:
266
+ try:
267
+ image = Image.open(uploaded_file)
268
+ st.image(image, caption="Uploaded Image", use_column_width=True)
269
+ query = st.text_area("Analysis requirements:", height=100)
270
+
271
+ if st.button("Analyze Image"):
272
+ if query:
273
+ image_base64 = image_to_base64(image)
274
+ if image_base64:
275
+ response = process_image_query(image_base64, query)
276
+ if response:
277
+ st.markdown('<div class="success-message">βœ… Image analyzed successfully!</div>', unsafe_allow_html=True)
278
+ st.markdown("### Analysis Results:")
279
+ st.write(response)
280
+ else:
281
+ st.warning("Please enter analysis requirements.")
282
+ except Exception as e:
283
+ st.error(f"Error processing image: {str(e)}")
284
+
285
+ # Footer
286
+ st.markdown("---")
287
+ st.markdown(
288
+ """
289
+ <div style='text-align: center; color: #666;'>
290
+ <p>TeleGuide - Your AI-Powered Telecommunications Assistant</p>
291
+ <p>Made with ❀️ for telecom professionals</p>
292
+ </div>
293
+ """,
294
+ unsafe_allow_html=True
295
+ )