File size: 12,278 Bytes
1bb0780
 
 
 
 
 
 
 
f173d1b
cb691e7
d3ed3b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bb0780
 
cb691e7
66bea2d
 
 
1bb0780
 
 
 
 
 
 
66bea2d
1bb0780
66bea2d
cb691e7
1bb0780
 
66bea2d
1bb0780
66bea2d
 
cb691e7
66bea2d
cb691e7
1bb0780
 
 
 
66bea2d
 
 
 
 
 
 
 
 
 
 
 
 
cb691e7
66bea2d
 
 
 
cb691e7
66bea2d
 
cb691e7
66bea2d
 
cb691e7
66bea2d
cb691e7
66bea2d
 
cb691e7
 
66bea2d
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
 
 
 
 
 
 
 
66bea2d
cb691e7
 
 
 
66bea2d
 
 
cb691e7
66bea2d
 
cb691e7
 
66bea2d
 
 
 
 
 
 
 
 
 
 
cb691e7
 
 
66bea2d
 
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
 
66bea2d
 
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
66bea2d
cb691e7
 
66bea2d
cb691e7
 
66bea2d
cb691e7
 
66bea2d
cb691e7
 
 
66bea2d
 
 
cb691e7
 
 
1bb0780
66bea2d
1bb0780
cb691e7
 
 
1bb0780
 
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import faiss
import numpy as np
import requests
import gradio as gr
from openai import OpenAI
from pypdf import PdfReader
from sentence_transformers import SentenceTransformer

css = """
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');
 
*, *::before, *::after { box-sizing: border-box; }
 
:root {
  --M:        #E20074;
  --MD:       #B5005C;
  --MBG:      rgba(226,0,116,0.07);
  --bg:       #F6F3F8;
  --white:    #FFFFFF;
  --surf2:    #F2EEF5;
  --border:   #E6DEED;
  --border2:  #D0C4DC;
  --text:     #1A1525;
  --sub:      #5A5272;
  --muted:    #9990AA;
  --ok:       #00A878;
  --okbg:     #E8F8F3;
  --okborder: #AADFC8;
  --f:        'Plus Jakarta Sans', sans-serif;
  --mono:     'JetBrains Mono', monospace;
  --sh-sm:    0 1px 4px rgba(0,0,0,0.06);
  --sh-md:    0 3px 14px rgba(0,0,0,0.08);
  --sh-mg:    0 4px 20px rgba(226,0,116,0.18);
}
 
/* ── BASE ── */
body {
  font-family: var(--f) !important;
  background: var(--bg) !important;
  color: var(--text) !important;
}
 
.gradio-container {
  max-width: 1060px !important;
  margin: 0 auto !important;
  padding: 0 20px 48px !important;
  background: transparent !important;
  box-shadow: none !important;
}
 
/* ── HEADING (gr.Markdown title) ── */
h1 {
  font-family: var(--f) !important;
  font-size: 1.6rem !important;
  font-weight: 800 !important;
  letter-spacing: -0.03em !important;
  color: var(--text) !important;
  padding: 28px 0 4px !important;
  border-bottom: 3px solid var(--M) !important;
  margin-bottom: 24px !important;
  position: relative;
}
 
/* animated stripe under title */
h1::after {
  content: '';
  position: absolute;
  bottom: -3px; left: 0;
  width: 80px; height: 3px;
  background: #FF6BB5;
  animation: titleSlide 2s ease-in-out infinite alternate;
}
@keyframes titleSlide {
  0%   { width: 60px; opacity: 0.6; }
  100% { width: 160px; opacity: 1; }
}
 
/* emoji in title stays natural */
h1 .emoji { color: inherit !important; }
 
/* ── ALL CARDS / PANELS ── */
.gr-box, .gr-panel, .gr-group, .gr-form,
[class*="panel"], [class*="block"] {
  background: var(--white) !important;
  border: 1px solid var(--border) !important;
  border-radius: 12px !important;
  box-shadow: var(--sh-sm) !important;
  transition: box-shadow 0.2s, border-color 0.2s !important;
}
.gr-box:hover, .gr-panel:hover {
  box-shadow: 0 4px 20px rgba(226,0,116,0.10) !important;
  border-color: rgba(226,0,116,0.22) !important;
}
 
/* ── LABELS ── */
label, .gr-label, .label-wrap span {
  font-family: var(--f) !important;
  font-size: 0.63rem !important;
  font-weight: 700 !important;
  letter-spacing: 0.10em !important;
  text-transform: uppercase !important;
  color: var(--muted) !important;
}
 
/* ── INPUTS (link box + question box) ── */
textarea, input[type="text"] {
  font-family: var(--f) !important;
  background: var(--surf2) !important;
  color: var(--text) !important;
  border: 1px solid var(--border2) !important;
  border-radius: 8px !important;
  padding: 12px 15px !important;
  font-size: 0.88rem !important;
  line-height: 1.55 !important;
  transition: border-color 0.2s, box-shadow 0.2s !important;
}
textarea:focus, input[type="text"]:focus {
  border-color: var(--M) !important;
  box-shadow: 0 0 0 3px rgba(226,0,116,0.12) !important;
  outline: none !important;
  background: var(--white) !important;
}
textarea::placeholder, input[type="text"]::placeholder {
  color: var(--muted) !important;
}
 
/* Status textbox (readonly) gets green monospace style */
textarea[readonly] {
  font-family: var(--mono) !important;
  font-size: 0.76rem !important;
  color: var(--ok) !important;
  background: var(--okbg) !important;
  border-color: var(--okborder) !important;
}
 
/* ── BUTTONS ── */
button, .gr-button {
  font-family: var(--f) !important;
  font-weight: 700 !important;
  font-size: 0.75rem !important;
  letter-spacing: 0.08em !important;
  text-transform: uppercase !important;
  border-radius: 8px !important;
  padding: 12px 24px !important;
  border: none !important;
  cursor: pointer !important;
  transition: all 0.2s ease !important;
}
 
/* Primary = magenta gradient */
button.primary, .gr-button-primary, button[variant="primary"] {
  background: linear-gradient(135deg, #E20074 0%, #B5005C 100%) !important;
  color: #fff !important;
  box-shadow: var(--sh-mg) !important;
}
button.primary:hover, .gr-button-primary:hover {
  box-shadow: 0 6px 28px rgba(226,0,116,0.40) !important;
  transform: translateY(-1px) !important;
}
button.primary:active, .gr-button-primary:active {
  transform: translateY(0) !important;
}
 
/* ── CHATBOT CONTAINER ── */
.gr-chatbot, [data-testid="chatbot"] {
  background: #FAFAFA !important;
  border: 1px solid var(--border) !important;
  border-radius: 12px !important;
  box-shadow: var(--sh-sm) !important;
}
 
/* User bubble β€” right, magenta */
.message.user, [data-testid="user"] .message {
  background: linear-gradient(135deg, #E20074, #B5005C) !important;
  color: #fff !important;
  border-radius: 12px 12px 3px 12px !important;
  padding: 11px 15px !important;
  font-size: 0.86rem !important;
  line-height: 1.65 !important;
  max-width: 72% !important;
  margin-left: auto !important;
  box-shadow: 0 3px 14px rgba(226,0,116,0.22) !important;
  animation: slideR 0.22s ease !important;
}
 
/* Bot bubble β€” left, white with magenta stripe */
.message.bot, [data-testid="bot"] .message {
  background: var(--white) !important;
  border: 1px solid var(--border) !important;
  border-left: 3px solid var(--M) !important;
  color: var(--text) !important;
  border-radius: 2px 12px 12px 12px !important;
  padding: 11px 15px !important;
  font-size: 0.86rem !important;
  line-height: 1.65 !important;
  max-width: 80% !important;
  box-shadow: var(--sh-sm) !important;
  animation: slideL 0.22s ease !important;
}
 
@keyframes slideR {
  from { opacity: 0; transform: translateX(10px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes slideL {
  from { opacity: 0; transform: translateX(-10px); }
  to   { opacity: 1; transform: translateX(0); }
}
 
/* ── SCROLLBAR ── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: var(--surf2); border-radius: 3px; }
::-webkit-scrollbar-thumb { background: rgba(226,0,116,0.28); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--M); }
 
/* ── HIDE GRADIO FOOTER ── */
footer { display: none !important; }
.gap { gap: 14px !important; }
"""

# ── GLOBALS ────────────────────────────────────────────────────────────────────
embed_model  = SentenceTransformer("all-MiniLM-L6-v2")
index        = None
chunks       = []
chat_history = []

client = OpenAI(
    api_key=os.getenv("GROQ_API_KEY"),
    base_url="https://api.groq.com/openai/v1",
)

def convert_drive_link(link):
    try:
        return f"https://drive.google.com/uc?id={link.split('/d/')[1].split('/')[0]}"
    except Exception:
        return link

def load_pdf_from_link(link):
    global index, chunks
    resp = requests.get(convert_drive_link(link), timeout=30)
    with open("temp.pdf", "wb") as f:
        f.write(resp.content)
    reader = PdfReader("temp.pdf")
    texts  = [p.extract_text() for p in reader.pages if p.extract_text()]
    chunks = []
    for t in texts:
        words = t.split()
        for i in range(0, len(words), 500):
            chunks.append(" ".join(words[i:i+500]))
    emb   = embed_model.encode(chunks)
    index = faiss.IndexFlatL2(emb.shape[1])
    index.add(np.array(emb, dtype="float32"))
    return f"βœ…  {len(chunks)} chunks indexed Β· {len(texts)} pages parsed Β· ready"

def retrieve(query, k=3):
    if index is None: return []
    q = embed_model.encode([query])
    _, idx = index.search(np.array(q, dtype="float32"), k)
    return [chunks[i] for i in idx[0]]

def generate_answer(query, ctx=""):
    if index is None:
        return "⚠️  Please load a PDF first."
    rag = "\n\n".join(retrieve(query))
    prompt = f"""You are a senior financial analyst at Deutsche Telekom.
You have memory of past conversation and access to official report data.

Conversation history:
{ctx}

Report excerpts (cite ONLY from these for facts):
{rag}

Question: {query}

Respond precisely, use numbers, and keep it concise."""
    resp = client.chat.completions.create(
        model="llama-3.1-8b-instant",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.4, max_tokens=650,
    )
    return resp.choices[0].message.content

def chat(user_input, history):
    global chat_history
    ctx = "\n".join(f"User: {h['user']}\nAssistant: {h['bot']}" for h in chat_history[-5:])
    answer = generate_answer(user_input, ctx)
    chat_history.append({"user": user_input, "bot": answer})
    new_history = history + [
        {"role": "user",      "content": user_input},
        {"role": "assistant", "content": answer},
    ]
    return new_history, new_history

LOGO = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Deutsche_Telekom_logo_simple.svg/120px-Deutsche_Telekom_logo_simple.svg.png"

with gr.Blocks(title="Telekom Finance RAG", css=css) as app:

    gr.HTML(f"""
    <div class="app-header">
      <div class="header-brand">
        <div class="t-logo">T</div>
        <div>
          <div class="header-title">Finance <em>RAG</em> Intelligence</div>
          <div class="header-sub">Deutsche Telekom Β· AI-Powered Document Analysis</div>
        </div>
      </div>
      <div class="status-pill">
        <div class="pulse-dot"></div>
        <span class="status-label">System Live</span>
      </div>
    </div>
    <div class="info-chips">
      <div class="chip"><div class="chip-dot"></div>Groq Β· LLaMA 3 8B</div>
      <div class="chip"><div class="chip-dot" style="background:#6600AA"></div>FAISS Vector Search</div>
      <div class="chip"><div class="chip-dot" style="background:#00C896"></div>MiniLM-L6 Embeddings</div>
      <div class="chip"><div class="chip-dot" style="background:#378ADD"></div>RAG Β· Top-K = 3</div>
      <div class="chip"><div class="chip-dot" style="background:#FFB700"></div>Conversation Memory</div>
    </div>
    """)

    gr.HTML('<div class="section-label">πŸ“Ž Document Loader</div>')

    with gr.Row(equal_height=True):
        with gr.Column(scale=6):
            link_input = gr.Textbox(
                label="Google Drive PDF Link",
                placeholder="https://drive.google.com/file/d/xxxxxxx/view",
            )
        with gr.Column(scale=1, min_width=150):
            load_btn = gr.Button("πŸ“₯  Load & Index", variant="primary")

    status = gr.Textbox(label="System Status", interactive=False, placeholder="Waiting for PDF…")

    gr.HTML("<div style='height:5px;background:linear-gradient(90deg,#E20074,transparent);border-radius:3px;opacity:.4;margin:16px 0 20px'></div>")

    gr.HTML('<div class="section-label">πŸ€– AI Financial Analyst</div>')

    chatbot = gr.Chatbot(height=460, show_label=False, avatar_images=(None, LOGO))

    with gr.Row(equal_height=True):
        with gr.Column(scale=8):
            msg = gr.Textbox(
                show_label=False,
                placeholder="Ask about revenue, EBITDA, KPIs, capex, strategic outlook…",
                container=False,
            )
        with gr.Column(scale=1, min_width=120):
            send_btn = gr.Button("Send ↑", variant="primary")

    gr.HTML("""
    <div class="app-footer">
      <div class="f-text">Powered by <span>Groq</span> Β· <span>LLaMA 3</span> Β· <span>FAISS</span> Β· <span>MiniLM</span></div>
      <div class="f-text">Deutsche Telekom AG Β· Internal AI Tool Β· Confidential</div>
    </div>
    """)

    load_btn.click(load_pdf_from_link, inputs=link_input, outputs=status)
    msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
    msg.submit(lambda: "", outputs=msg)
    send_btn.click(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
    send_btn.click(lambda: "", outputs=msg)

if __name__ == "__main__":
    app.launch()