File size: 2,954 Bytes
a28f0ae
 
 
f9002bb
a28f0ae
f9002bb
58b1acd
a28f0ae
79c01c2
a28f0ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79c01c2
a28f0ae
 
f9002bb
 
 
a28f0ae
 
f9002bb
a28f0ae
 
f9002bb
 
 
a28f0ae
 
f9002bb
 
a28f0ae
f9002bb
 
 
a28f0ae
f9002bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28f0ae
 
 
f9002bb
 
a28f0ae
 
 
 
 
 
 
f9002bb
a28f0ae
f9002bb
a28f0ae
 
 
58b1acd
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
import gradio as gr
import requests
import os
from PyPDF2 import PdfReader

# Add your DeepSeek API Key
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "your-key-here")


def extract_pdf_text(pdf_file):
    reader = PdfReader(pdf_file)
    text = ""
    for page in reader.pages:
        text += page.extract_text() or ""
    return text.strip()

def ask_pdf(pdf_file, question):
    if not pdf_file or not question:
        return "⚠️ Please upload a PDF and enter a question."

    content = extract_pdf_text(pdf_file)
    prompt = f"Answer this question based on the following PDF content:\n\n{content[:4000]}\n\nQuestion: {question}"

    headers = {
        "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "deepseek-chat",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ]
    }

    try:
        response = requests.post("https://api.deepseek.com/chat/completions", json=data, headers=headers)
        result = response.json()
        return result['choices'][0]['message']['content']
    except Exception as e:
        return f"❌ Error: {e}"


with gr.Blocks(css="""
    body {
        background-color: #111;
        color: #f5f5f5;
        font-family: 'Inter', sans-serif;
    }
    .gr-button {
        background: #6a0dad;
        color: white;
        font-weight: bold;
        border-radius: 8px;
        border: 2px solid #6a0dad;
        transition: 0.3s;
    }
    .gr-button:hover {
        background: #8e44ad;
        border-color: #8e44ad;
    }
    .gr-box, .gr-textbox, .gr-file {
        background-color: #1c1c1c;
        border: 1px solid #333;
        border-radius: 10px;
        color: white;
    }
    footer {
        visibility: hidden;
    }
    .title {
        font-size: 2.4rem;
        text-align: center;
        font-weight: 700;
        margin-top: 1.5rem;
        background: linear-gradient(to right, #ff4ecb, #8873f5);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    .subtitle {
        font-size: 1.2rem;
        text-align: center;
        color: #aaa;
        margin-bottom: 2rem;
    }
""") as demo:
    gr.Markdown("""
    <div class="title">✨ PDF Genius Chatbot</div>
    <div class="subtitle">Ask your PDFs anything β€” powered by DeepSeek AI</div>
    """)

    with gr.Row():
        with gr.Column():
            pdf_input = gr.File(label="πŸ“„ Upload your PDF", file_types=[".pdf"])
            question_input = gr.Textbox(label="❓ Ask a question about your PDF", placeholder="e.g. Summarize this document...", lines=2)
            btn = gr.Button("πŸš€ Ask the PDF")

        with gr.Column():
            output = gr.Textbox(label="πŸ’‘ Answer", lines=14)

    btn.click(fn=ask_pdf, inputs=[pdf_input, question_input], outputs=output)

demo.launch()