agnixcode commited on
Commit
9cb50b0
·
verified ·
1 Parent(s): c238304

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -0
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import faiss
3
+ import numpy as np
4
+ import requests
5
+ import gradio as gr
6
+
7
+ from openai import OpenAI
8
+ from pypdf import PdfReader
9
+ from sentence_transformers import SentenceTransformer
10
+
11
+ # =========================
12
+ # INIT
13
+ # =========================
14
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
15
+ index = None
16
+ chunks = []
17
+
18
+ client = OpenAI(
19
+ api_key=os.getenv("GROQ_API_KEY"),
20
+ base_url="https://api.groq.com/openai/v1",
21
+ )
22
+
23
+ # =========================
24
+ # TELEKOM THEME LOGO
25
+ # =========================
26
+ LOGO_URL = "https://www.telekom.com/resource/image/1037468/landscape_ratio16x9/1920/1080/0a6b8d7f3d9f6f0c5f4c8b9c2c1b2d3e/telekom-logo.jpg"
27
+
28
+ # =========================
29
+ # DRIVE LINK HANDLER
30
+ # =========================
31
+ def convert_drive_link(link):
32
+ try:
33
+ file_id = link.split("/d/")[1].split("/")[0]
34
+ return f"https://drive.google.com/uc?id={file_id}"
35
+ except:
36
+ return link
37
+
38
+ # =========================
39
+ # LOAD PDF
40
+ # =========================
41
+ def load_pdf_from_link(link):
42
+ global index, chunks
43
+
44
+ url = convert_drive_link(link)
45
+ pdf_path = "temp.pdf"
46
+
47
+ r = requests.get(url)
48
+ with open(pdf_path, "wb") as f:
49
+ f.write(r.content)
50
+
51
+ reader = PdfReader(pdf_path)
52
+
53
+ texts = []
54
+ for page in reader.pages:
55
+ text = page.extract_text()
56
+ if text:
57
+ texts.append(text)
58
+
59
+ chunks = []
60
+ for t in texts:
61
+ words = t.split()
62
+ for i in range(0, len(words), 500):
63
+ chunks.append(" ".join(words[i:i+500]))
64
+
65
+ embeddings = embed_model.encode(chunks)
66
+ dim = embeddings.shape[1]
67
+
68
+ index = faiss.IndexFlatL2(dim)
69
+ index.add(np.array(embeddings))
70
+
71
+ return f"✅ PDF Loaded | Chunks: {len(chunks)}"
72
+
73
+ # =========================
74
+ # RETRIEVAL
75
+ # =========================
76
+ def retrieve(query, k=3):
77
+ q_emb = embed_model.encode([query])
78
+ _, indices = index.search(np.array(q_emb), k)
79
+ return [chunks[i] for i in indices[0]]
80
+
81
+ # =========================
82
+ # GENERATION
83
+ # =========================
84
+ def generate_answer(query):
85
+ if index is None:
86
+ return "⚠️ Load a PDF first"
87
+
88
+ context = "\n\n".join(retrieve(query))
89
+
90
+ prompt = f"""
91
+ You are a professional AI assistant.
92
+ Answer ONLY from context.
93
+
94
+ Context:
95
+ {context}
96
+
97
+ Question:
98
+ {query}
99
+ """
100
+
101
+ res = client.responses.create(
102
+ model="openai/gpt-oss-20b",
103
+ input=prompt
104
+ )
105
+
106
+ return res.output_text
107
+
108
+ # =========================
109
+ # CHAT
110
+ # =========================
111
+ def chat(msg, history):
112
+ ans = generate_answer(msg)
113
+ history.append((msg, ans))
114
+ return history, history
115
+
116
+ # =========================
117
+ # CSS (BOTPRESS + TELEKOM STYLE)
118
+ # =========================
119
+ css = """
120
+ body {
121
+ background-color: #0b0b10;
122
+ color: white;
123
+ font-family: 'Inter', sans-serif;
124
+ }
125
+
126
+ .gradio-container {
127
+ max-width: 1100px !important;
128
+ }
129
+
130
+ h1 {
131
+ text-align: center;
132
+ color: #e20074; /* Telekom pink */
133
+ font-weight: 700;
134
+ }
135
+
136
+ .chatbot {
137
+ background: #11131a !important;
138
+ border-radius: 15px;
139
+ border: 1px solid #2a2d3a;
140
+ }
141
+
142
+ button {
143
+ background: linear-gradient(90deg, #e20074, #ff4da6) !important;
144
+ border-radius: 10px !important;
145
+ color: white !important;
146
+ font-weight: bold;
147
+ transition: 0.3s;
148
+ }
149
+
150
+ button:hover {
151
+ transform: scale(1.03);
152
+ box-shadow: 0 0 15px #e20074;
153
+ }
154
+
155
+ input {
156
+ background: #1a1d26 !important;
157
+ border: 1px solid #2a2d3a !important;
158
+ color: white !important;
159
+ border-radius: 10px !important;
160
+ }
161
+
162
+ .card {
163
+ background: #11131a;
164
+ padding: 15px;
165
+ border-radius: 15px;
166
+ border: 1px solid #2a2d3a;
167
+ }
168
+ """
169
+
170
+ # =========================
171
+ # UI
172
+ # =========================
173
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as app:
174
+
175
+ gr.Image(LOGO_URL, height=80)
176
+ gr.Markdown("# 📊 Telekom AI RAG Chatbot")
177
+
178
+ with gr.Row():
179
+ pdf_link = gr.Textbox(label="📎 PDF Link (Google Drive supported)")
180
+ load_btn = gr.Button("Load Document")
181
+
182
+ status = gr.Textbox()
183
+
184
+ chatbot = gr.Chatbot()
185
+ state = gr.State([])
186
+
187
+ msg = gr.Textbox(label="Ask Anything")
188
+
189
+ load_btn.click(load_pdf_from_link, inputs=pdf_link, outputs=status)
190
+ msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
191
+
192
+ app.launch()