File size: 3,999 Bytes
6423f29 e102bb8 6423f29 e102bb8 6423f29 | 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 |
import gradio as gr
import modal
import requests
import spaces
# Modal Function
rag = modal.Cls.from_name(
"minicpm-rag",
"RAG"
)()
pdf_bytes_global = None
def upload_pdf(pdf):
if pdf is None:
return "β Please upload a PDF"
with open(pdf.name, "rb") as f:
pdf_bytes = f.read()
r = requests.post(
"https://gajanand1902--minicpm-rag-upload-pdf.modal.run",
json={"pdf_bytes": list(pdf_bytes)}
)
data = r.json()
if isinstance(data, dict):
return str(data)
return data
@spaces.GPU
def chat(message, history):
r = requests.post(
"https://gajanand1902--minicpm-rag-chat-api.modal.run",
json={"question": message}
)
data = r.json()
if isinstance(data, dict):
return data.get("answer", str(data))
return str(data)
# Custom Theme
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="cyan",
neutral_hue="slate"
)
css = """
.gradio-container {
max-width: none !important;
width: 100% !important;
margin: 0 !important;
}
.upload-card {
width: 100% !important;
max-width: none !important;
padding: 20px;
border-radius: 15px;
background: #f8fafc;
border: 1px solid #e5e7eb;
}
padding: 20px;
border-radius: 15px;
background: #f8fafc;
border: 1px solid #e5e7eb;
}
.footer {
text-align:center;
color:gray;
}
button {
border-radius: 10px !important;
}
.chatbot {
border-radius: 15px !important;
}
.chat-section textarea {
background: #ffffff !important;
border: 2px solid #2563eb !important;
border-radius: 16px !important;
padding: 14px !important;
font-size: 15px !important;
}
.chat-section textarea:focus {
border-color: #06b6d4 !important;
box-shadow: 0 0 10px rgba(6,182,212,0.4) !important;
}
.chat-section textarea::placeholder {
color: #64748b !important;
opacity: 1 !important;
font-weight: 500;
}
"""
with gr.Blocks(
title="π MiniCPM Financial RAG",
theme=theme,
css=css
) as demo:
# Header
gr.HTML("""
<div style="
text-align:center;
padding:20px;
background:linear-gradient(90deg,#2563eb,#06b6d4);
color:white;
border-radius:15px;
margin-bottom:20px;"
>
</div>
""")
# Layout: left upload, right chat
with gr.Row():
# Left side (smaller header + upload)
with gr.Column(scale=1):
# Small header
gr.HTML("""
<div class="header" style="text-align:center;padding:8px;background:linear-gradient(90deg,#2563eb,#06b6d4);color:white;border-radius:12px;margin-bottom:8px;">
<h1 style="font-size:1.2rem;margin:0;">π MiniCPM Financial QA RAG</h1>
<p style="font-size:0.9rem;margin:0;">Upload a Financial PDF and Chat with it using AI</p>
</div>
""")
with gr.Group(elem_classes="upload-card"):
gr.Markdown("### π Upload Document")
pdf = gr.File(label="Choose PDF", file_types=[".pdf"])
upload_btn = gr.Button("π Process PDF", variant="primary")
status = gr.Textbox(label="π Status", interactive=False)
upload_btn.click(fn=upload_pdf, inputs=pdf, outputs=status)
# Right side (chat with scrollbar)
with gr.Column(scale=3):
gr.Markdown("### π¬ Ask Questions")
with gr.Group(elem_classes="chat-section"):
gr.ChatInterface(
fn=chat,
chatbot=gr.Chatbot(
height=600,
show_label=False
),
textbox=gr.Textbox(
placeholder="π€ Ask a question about your financial report...",
container=False,
scale=7
)
)
if __name__ == "__main__":
demo.launch()
|