File size: 17,568 Bytes
828c581
 
 
e570aec
828c581
 
 
 
 
 
 
 
 
e6fa2c9
828c581
 
0706a4a
44f2d27
3ae6171
828c581
826da33
828c581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44f2d27
828c581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b076ef
828c581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44f2d27
828c581
e6fa2c9
 
 
 
 
 
 
828c581
 
e6fa2c9
 
828c581
 
e6fa2c9
828c581
 
e6fa2c9
828c581
e6fa2c9
 
828c581
 
e6fa2c9
828c581
e6fa2c9
828c581
e6fa2c9
828c581
 
 
 
 
e6fa2c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44f2d27
828c581
 
 
e6fa2c9
828c581
 
e6fa2c9
828c581
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6fa2c9
828c581
 
 
 
 
 
 
 
 
 
 
 
44f2d27
 
 
 
 
 
 
 
 
828c581
44f2d27
828c581
44f2d27
 
828c581
 
 
 
 
 
44f2d27
828c581
 
44f2d27
828c581
44f2d27
828c581
44f2d27
828c581
 
 
44f2d27
 
 
580b2aa
 
44f2d27
580b2aa
826da33
580b2aa
826da33
580b2aa
44f2d27
826da33
 
580b2aa
 
 
 
 
 
 
 
826da33
580b2aa
 
826da33
580b2aa
 
826da33
580b2aa
 
826da33
580b2aa
826da33
580b2aa
 
 
826da33
580b2aa
3ae6171
580b2aa
 
3ae6171
828c581
580b2aa
828c581
580b2aa
 
44f2d27
580b2aa
 
 
 
44f2d27
580b2aa
828c581
 
580b2aa
 
 
 
 
828c581
580b2aa
 
828c581
580b2aa
826da33
580b2aa
828c581
 
 
580b2aa
828c581
3ae6171
580b2aa
 
 
3ae6171
828c581
580b2aa
828c581
580b2aa
 
 
828c581
 
 
 
 
826da33
828c581
 
 
 
 
 
580b2aa
828c581
 
580b2aa
 
 
 
826da33
580b2aa
 
828c581
 
580b2aa
 
 
 
826da33
580b2aa
 
 
 
 
 
 
828c581
580b2aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from fastapi import FastAPI, UploadFile, File, Query
from fastapi.middleware.cors import CORSMiddleware
import nomic
import pymupdf as fitz # PyMuPDF
from nomic import embed
from pinecone import Pinecone, Vector
import os
from dotenv import load_dotenv
import uuid
import requests
from bs4 import BeautifulSoup
import base64
import io
from urllib.parse import urljoin
from PIL import Image
from urllib.parse import urlparse, parse_qs
import time
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript
from groq import Groq
import uuid
load_dotenv()

# -------- CLIENTS --------
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))

from google import genai # 

# Client initialize karein
client_gemini = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))


PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
NOMIC_API_KEY = os.getenv("NOMIC_API_KEY")

if NOMIC_API_KEY:
    nomic.login(NOMIC_API_KEY)
else:
    print("Warning: NOMIC_API_KEY not found!")

pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index("context-hub")

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# -------- NAMESPACES --------
PDF_NAMESPACE = "pdf"  
current_pdf_namespace = "pdf_default"
IMAGE_NAMESPACE = "image"
YOUTUBE_NAMESPACE = "youtube"

# -------- HELPER FUNCTIONS --------

def chunk_text(text: str, size: int = 500):
    return [text[i:i+size] for i in range(0, len(text), size)]


def create_embeddings(chunks: list):
    if not chunks:
        return []
    try:
        response = embed.text(
            texts=chunks,
            model="nomic-embed-text-v1",
            task_type="search_document"
        )
        result = dict(response)
        if 'embeddings' in result and len(result['embeddings']) > 0:
            return result['embeddings']
        else:
            print("DEBUG: Nomic returned no embeddings!")
            return []
    except Exception as e:
        print(f"Embedding error: {e}")
        return []


def store_embeddings(chunks: list, embeddings: list, namespace: str = ""):
    if not chunks or not embeddings:
        print("DEBUG: Nothing to store.")
        return
    vectors = [
        Vector(
            id=str(uuid.uuid4()),
            values=embeddings[i],
            metadata={"text": chunks[i]}
        )
        for i in range(len(embeddings))
    ]
    if vectors:
        index.upsert(vectors=vectors, namespace=namespace)   # ← namespace added


def search(query: str, namespace: str = ""):
    try:
        query_response = embed.text(
            texts=[query],
            model="nomic-embed-text-v1",
            task_type="search_query"
        )
        result = dict(query_response)
        query_emb = result['embeddings'][0]

        results = index.query(
            vector=query_emb,
            top_k=3,
            include_metadata=True,
            namespace=namespace    # ← namespace added
        )
        relevant_chunks = []
        for match in results['matches']:
            if match.get('metadata') and 'text' in match['metadata']:
                relevant_chunks.append(match['metadata']['text'])
        return relevant_chunks

    except Exception as e:
        print(f"Search error: {e}")
        return []


def generate_answer(query: str, context: str):
    try:
        response = groq_client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[
                {
                    "role": "system",
                    "content": (
                        "You are a helpful assistant. Answer ONLY using the provided context. "
                        "If the answer is not in the context, say you don't know. "
                        "When you find an answer, elaborate and provide more information."
                    )
                },
                {
                    "role": "user",
                    "content": f"Context: {context}\n\nQuestion: {query}"
                }
            ]
        )
        return response.choices[0].message.content

    except Exception as e:
        print(f"Groq Error: {str(e)}")
        return f"Groq Error: {str(e)}"

@app.post("/upload")
async def upload_pdf(file: UploadFile = File(...)):
    global current_pdf_namespace
    try:
        # Generate a fresh unique namespace for every new upload
        current_pdf_namespace = f"pdf_{uuid.uuid4().hex}"
        print(f"DEBUG: Using new namespace: {current_pdf_namespace}")

        content = await file.read()
        pdf = fitz.open(stream=content, filetype="pdf")

        text = ""
        for page in pdf:
            text += page.get_text()

        if not text.strip():
            return {"error": "PDF is empty or could not be read."}

        chunks = chunk_text(text)
        embeddings = create_embeddings(chunks)
        store_embeddings(chunks, embeddings, namespace=current_pdf_namespace)
        return {"message": "PDF processed successfully."}

    except Exception as e:
        print(f"Upload Error: {e}")
        return {"error": f"Internal Server Error: {str(e)}"}
    
    
@app.post("/query")
async def query_pdf(query: str = Query(...)):
    global current_pdf_namespace
    try:
        results = search(query, namespace=current_pdf_namespace)
        if not results:
            return {"answer": "I couldn't find any relevant information in the document."}
        context = " ".join(results)
        answer = generate_answer(query, context)
        return {"answer": answer}
    except Exception as e:
        return {"error": f"Search failed: {str(e)}"}
    
# -------- IMAGE HELPERS -------------------------------------------------------------------------------------------------------------------

def create_image_embedding(image):
    output = embed.image(
        images=[image],
        model="nomic-embed-vision-v1.5"
    )
    result = dict(output)
    return result["embeddings"][0]


def store_image_embedding(image_vector, filename: str):
    index.upsert(
        vectors=[Vector(
            id=str(uuid.uuid4()),
            values=image_vector,
            metadata={"type": "image", "filename": filename}
        )],
        namespace=IMAGE_NAMESPACE
    )

def generate_image_answer(query, image):
    try:
        # Convert image β†’ bytes
        img_byte_arr = io.BytesIO()
        image.save(img_byte_arr, format="PNG")
        img_bytes = img_byte_arr.getvalue()

        # Convert to base64
        img_base64 = base64.b64encode(img_bytes).decode("utf-8")

        response = client_gemini.models.generate_content(
            model="gemini-2.5-flash",
            contents=[
                {
                    "role": "user",
                    "parts": [
                        {"text": query},
                        {
                            "inline_data": {
                                "mime_type": "image/png",
                                "data": img_base64
                            }
                        }
                    ]
                }
            ]
        )

        return response.text if response.text else "No answer generated."

    except Exception as e:
        return f"Error: {str(e)}"
    


# -------- URL HELPERS --------------------------------------------------------------------------------------------------------------

def normalize_url(url: str):
    url = url.strip()
    if not url.startswith(("http://", "https://")):
        url = "https://" + url
    return url


def extract_text_from_url(url: str):
    try:
        url = normalize_url(url)   # βœ… FIX ADDED

        headers = {"User-Agent": "Mozilla/5.0"}
        response = requests.get(url, headers=headers, timeout=10)

        if response.status_code != 200:
            return None

        soup = BeautifulSoup(response.text, "html.parser")

        # Remove unwanted tags
        for tag in soup(["script", "style"]):
            tag.decompose()

        text = soup.get_text(separator=" ")

        return text[:5000]

    except Exception as e:
        print(f"URL Error: {e}")
        return None


def extract_about_contact(base_url: str):
    """Optional enhancement: fetch About & Contact pages"""
    try:
        base_url = normalize_url(base_url)

        headers = {"User-Agent": "Mozilla/5.0"}
        response = requests.get(base_url, headers=headers, timeout=10)
        soup = BeautifulSoup(response.text, "html.parser")

        links = [a.get("href") for a in soup.find_all("a", href=True)]

        about_url = None
        contact_url = None

        for link in links:
            full_link = urljoin(base_url, link)

            if "about" in link.lower() and not about_url:
                about_url = full_link

            if "contact" in link.lower() and not contact_url:
                contact_url = full_link

        content = ""

        if about_url:
            content += extract_text_from_url(about_url) or ""

        if contact_url:
            content += extract_text_from_url(contact_url) or ""

        return content[:5000]

    except:
        return ""


def generate_url_answer(text: str, url: str, query: str = None):
    try:
        user_msg = (
            f"URL: {url}\n\n"
            f"Content:\n{text[:5000]}\n\n"
            f"Task: Explain what this page is about in simple words."
        )

        if query:
            user_msg += f"\n\nUser Question: {query}"

        response = groq_client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[
                {
                    "role": "system",
                    "content": "You are a helpful assistant that answers questions about webpage content."
                },
                {
                    "role": "user",
                    "content": user_msg
                }
            ]
        )

        return response.choices[0].message.content

    except Exception as e:
        return f"Error: {str(e)}"


current_url = None
current_url_text = None

@app.post("/process-url")
async def process_url(url: str = Query(...)):
    global current_url, current_url_text

    url = normalize_url(url)

    main_text = extract_text_from_url(url)
    extra_text = extract_about_contact(url)

    combined_text = (main_text or "") + "\n\n" + (extra_text or "")

    if not combined_text.strip():
        return {"error": "Failed to extract content"}

    current_url = url
    current_url_text = combined_text   # βœ… store everything once

    return {"message": "URL processed successfully"}


@app.post("/query-url")
async def query_url(query: str = Query(...)):
    global current_url, current_url_text

    if not current_url_text:
        return {"error": "No URL processed"}

    try:
        answer = generate_url_answer(current_url_text, current_url, query)
        return {"answer": answer}

    except Exception as e:
        return {"error": str(e)}

        
# ########################################                  YOUTUBE                    ############################################

import yt_dlp
import whisper

def get_video_id(url: str):
    parsed_url = urlparse(url)
    if "youtube.com" in url:
        return parse_qs(parsed_url.query).get("v", [None])[0]
    elif "youtu.be" in url:
        return parsed_url.path.strip("/")
    return None

def get_transcript_whisper(url):
    try:
        # Download audio
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': 'audio.%(ext)s',
            'quiet': True
        }

        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])

        # Load Whisper model
        model = whisper.load_model("base")  # use "small" or "medium" for better accuracy

        # Transcribe
        result = model.transcribe("audio.webm")

        return result["text"]

    except Exception as e:
        return f"ERROR: {str(e)}"
    

from pydantic import BaseModel

class YouTubeRequest(BaseModel):
    url: str

@app.post("/process-youtube")
async def process_youtube(request: YouTubeRequest):
    try:
        url = request.url
        video_id = get_video_id(url)

        text = get_transcript_whisper(url)
        if text.startswith("ERROR"):
            print("TRANSCRIPT ERROR:", text)  
            return {"status": "error", "error": text}

        chunks = chunk_text(text, size=1000)
        embeddings = create_embeddings(chunks)

        vectors = [
            Vector(
                id=str(uuid.uuid4()),
                values=embeddings[i],
                metadata={"text": chunks[i]}
            )
            for i in range(len(embeddings))
        ]

        index.upsert(vectors=vectors, namespace=video_id)

        return {"status": "success"}

    except Exception as e:
        return {"error": str(e)}
    


class QueryRequest(BaseModel):
    query: str
    video_id: str

@app.post("/query-youtube")
async def query_youtube(request: QueryRequest):
    try:
        query = request.query
        video_id = request.video_id

        query_response = embed.text(
            texts=[query],
            model="nomic-embed-text-v1",
            task_type="search_query"
        )

        query_emb = dict(query_response)["embeddings"][0]

        results = index.query(
            vector=query_emb,
            top_k=5,
            include_metadata=True,
            namespace=video_id
        )

        context = "\n\n".join([
            match["metadata"]["text"]
            for match in results["matches"]
        ])

        if not context.strip():
            return {"answer": "No relevant info found."}

        answer = generate_answer(query, context)
        return {"answer": answer}
    
    except Exception as e:
        return {"error": str(e)}

# -------------------------------
# Get transcript (SAFE)
# -------------------------------
# def fetch_transcript(video_id: str):
#     # βœ… CACHE
#     if video_id in transcript_cache:
#         return transcript_cache[video_id]

#     # βœ… NEW API: use instance method
#     ytt_api = YouTubeTranscriptApi()
#     fetched = ytt_api.fetch(video_id, languages=['en'])

#     processed = [
#         {
#             "text": snippet.text,
#             "start": snippet.start
#         }
#         for snippet in fetched
#     ]

#     transcript_cache[video_id] = processed
#     return processed


# @app.post("/process-youtube")
# async def process_youtube(url: str = Query(...)):
#     try:
#         video_id = extract_video_id(url)

#         if not video_id:
#             return {"error": "Invalid YouTube URL"}

#         # βœ… RATE LIMIT
#         if not is_allowed(video_id):
#             return {"error": "Too many requests. Please wait a few seconds."}

#         # βœ… PREVENT DUPLICATE PROCESSING
#         if video_id in processing_videos:
#             return {"message": "Already processing this video"}

#         processing_videos.add(video_id)

#         try:
#             transcript_data = fetch_transcript(video_id)

#         except TranscriptsDisabled:
#             return {"error": "Captions disabled for this video"}

#         except NoTranscriptFound:
#             return {"error": "No transcript available"}

#         except Exception as e:
#             return {"error": f"Transcript error: {str(e)}"}

#         # Combine text
#         full_text = " ".join([item["text"] for item in transcript_data])

#         chunks = chunk_text(full_text, size=1000)
#         embeddings = create_embeddings(chunks)

#         if not embeddings:
#             return {"error": "Failed to create embeddings"}

#         vectors = []
#         for i, chunk in enumerate(chunks):
#             vectors.append(
#                 Vector(
#                     id=f"{video_id}_{i}",
#                     values=embeddings[i],
#                     metadata={
#                         "text": chunk,
#                         "source": "youtube",
#                         "chunk_id": i,
#                         "video_id": video_id
#                     }
#                 )
#             )

#         index.upsert(vectors=vectors, namespace=YOUTUBE_NAMESPACE)

#         return {"message": f"Processed successfully ({len(chunks)} chunks)"}

#     except Exception as e:
#         return {"error": str(e)}

#     finally:
#         if 'video_id' in locals():
#             processing_videos.discard(video_id)


# @app.post("/query-youtube")
# async def query_youtube(query: str = Query(...)):
#     try:
#         query_response = embed.text(
#             texts=[query],
#             model="nomic-embed-text-v1",
#             task_type="search_query"
#         )

#         query_emb = dict(query_response)["embeddings"][0]

#         results = index.query(
#             vector=query_emb,
#             top_k=5,
#             include_metadata=True,
#             namespace=YOUTUBE_NAMESPACE
#         )

#         matches = results.get("matches", [])

#         if not matches:
#             return {"answer": "No relevant content found in this video."}

#         context = "\n\n".join([m["metadata"]["text"] for m in matches])

#         answer = generate_answer(query, context)

#         return {
#             "answer": answer,
#             "sources": [
#                 {"chunk": m["metadata"]["chunk_id"]}
#                 for m in matches
#             ]
#         }

#     except Exception as e:
#         return {"error": str(e)}