Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,11 +20,6 @@ client = OpenAI(
|
|
| 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 |
# =========================
|
|
@@ -68,7 +63,7 @@ def load_pdf_from_link(link):
|
|
| 68 |
index = faiss.IndexFlatL2(dim)
|
| 69 |
index.add(np.array(embeddings))
|
| 70 |
|
| 71 |
-
return f"β
PDF Loaded | Chunks: {len(chunks)}"
|
| 72 |
|
| 73 |
# =========================
|
| 74 |
# RETRIEVAL
|
|
@@ -83,13 +78,13 @@ def retrieve(query, k=3):
|
|
| 83 |
# =========================
|
| 84 |
def generate_answer(query):
|
| 85 |
if index is None:
|
| 86 |
-
return "β οΈ
|
| 87 |
|
| 88 |
context = "\n\n".join(retrieve(query))
|
| 89 |
|
| 90 |
prompt = f"""
|
| 91 |
You are a professional AI assistant.
|
| 92 |
-
Answer ONLY
|
| 93 |
|
| 94 |
Context:
|
| 95 |
{context}
|
|
@@ -106,11 +101,11 @@ Question:
|
|
| 106 |
return res.output_text
|
| 107 |
|
| 108 |
# =========================
|
| 109 |
-
# CHAT
|
| 110 |
# =========================
|
| 111 |
def chat(msg, history):
|
| 112 |
-
|
| 113 |
-
history.append((msg,
|
| 114 |
return history, history
|
| 115 |
|
| 116 |
# =========================
|
|
@@ -120,7 +115,7 @@ css = """
|
|
| 120 |
body {
|
| 121 |
background-color: #0b0b10;
|
| 122 |
color: white;
|
| 123 |
-
font-family:
|
| 124 |
}
|
| 125 |
|
| 126 |
.gradio-container {
|
|
@@ -129,7 +124,7 @@ body {
|
|
| 129 |
|
| 130 |
h1 {
|
| 131 |
text-align: center;
|
| 132 |
-
color: #e20074;
|
| 133 |
font-weight: 700;
|
| 134 |
}
|
| 135 |
|
|
@@ -158,35 +153,39 @@ input {
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="π
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
base_url="https://api.groq.com/openai/v1",
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# =========================
|
| 24 |
# DRIVE LINK HANDLER
|
| 25 |
# =========================
|
|
|
|
| 63 |
index = faiss.IndexFlatL2(dim)
|
| 64 |
index.add(np.array(embeddings))
|
| 65 |
|
| 66 |
+
return f"β
PDF Loaded Successfully | Chunks: {len(chunks)}"
|
| 67 |
|
| 68 |
# =========================
|
| 69 |
# RETRIEVAL
|
|
|
|
| 78 |
# =========================
|
| 79 |
def generate_answer(query):
|
| 80 |
if index is None:
|
| 81 |
+
return "β οΈ Please load a PDF first."
|
| 82 |
|
| 83 |
context = "\n\n".join(retrieve(query))
|
| 84 |
|
| 85 |
prompt = f"""
|
| 86 |
You are a professional AI assistant.
|
| 87 |
+
Answer ONLY using the given context.
|
| 88 |
|
| 89 |
Context:
|
| 90 |
{context}
|
|
|
|
| 101 |
return res.output_text
|
| 102 |
|
| 103 |
# =========================
|
| 104 |
+
# CHAT FUNCTION
|
| 105 |
# =========================
|
| 106 |
def chat(msg, history):
|
| 107 |
+
answer = generate_answer(msg)
|
| 108 |
+
history.append((msg, answer))
|
| 109 |
return history, history
|
| 110 |
|
| 111 |
# =========================
|
|
|
|
| 115 |
body {
|
| 116 |
background-color: #0b0b10;
|
| 117 |
color: white;
|
| 118 |
+
font-family: Inter, sans-serif;
|
| 119 |
}
|
| 120 |
|
| 121 |
.gradio-container {
|
|
|
|
| 124 |
|
| 125 |
h1 {
|
| 126 |
text-align: center;
|
| 127 |
+
color: #e20074;
|
| 128 |
font-weight: 700;
|
| 129 |
}
|
| 130 |
|
|
|
|
| 153 |
color: white !important;
|
| 154 |
border-radius: 10px !important;
|
| 155 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
"""
|
| 157 |
|
| 158 |
# =========================
|
| 159 |
# UI
|
| 160 |
# =========================
|
| 161 |
+
with gr.Blocks() as app:
|
| 162 |
+
|
| 163 |
+
# LOGO (FIXED β NO gr.Image)
|
| 164 |
+
gr.HTML("""
|
| 165 |
+
<div style="display:flex;justify-content:center;margin-bottom:10px;">
|
| 166 |
+
<img src="https://www.telekom.com/resource/image/1037468/landscape_ratio16x9/1920/1080/0a6b8d7f3d9f6f0c5f4c8b9c2c1b2d3e/telekom-logo.jpg"
|
| 167 |
+
width="180"/>
|
| 168 |
+
</div>
|
| 169 |
+
""")
|
| 170 |
|
|
|
|
| 171 |
gr.Markdown("# π Telekom AI RAG Chatbot")
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
+
pdf_link = gr.Textbox(label="π Google Drive PDF Link")
|
| 175 |
load_btn = gr.Button("Load Document")
|
| 176 |
|
| 177 |
+
status = gr.Textbox(label="Status")
|
| 178 |
|
| 179 |
chatbot = gr.Chatbot()
|
| 180 |
state = gr.State([])
|
| 181 |
|
| 182 |
msg = gr.Textbox(label="Ask Anything")
|
| 183 |
|
| 184 |
+
# Actions
|
| 185 |
load_btn.click(load_pdf_from_link, inputs=pdf_link, outputs=status)
|
| 186 |
msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
|
| 187 |
|
| 188 |
+
# =========================
|
| 189 |
+
# LAUNCH (Gradio 6 FIX)
|
| 190 |
+
# =========================
|
| 191 |
+
app.launch(css=css, theme=gr.themes.Soft())
|