Update app.py
Browse files
app.py
CHANGED
|
@@ -29,7 +29,7 @@ def ui_set_api_key(api_key: str):
|
|
| 29 |
return None, "❌ API key required."
|
| 30 |
try:
|
| 31 |
client = genai.Client(api_key=api_key)
|
| 32 |
-
return client, "✅ API key set."
|
| 33 |
except Exception as e:
|
| 34 |
return None, f"❌ Failed to set API key: {e}"
|
| 35 |
|
|
@@ -38,7 +38,7 @@ def upload_and_index(client_state, file_obj, progress=gr.Progress(track_tqdm=Tru
|
|
| 38 |
yield None, "❌ Set API key first.", _progress_html(0, "Waiting for API key")
|
| 39 |
return
|
| 40 |
if file_obj is None:
|
| 41 |
-
yield None, "⚠️ Please upload a file.", _progress_html(0, "Waiting")
|
| 42 |
return
|
| 43 |
|
| 44 |
client = _require_client(client_state)
|
|
@@ -60,6 +60,7 @@ def upload_and_index(client_state, file_obj, progress=gr.Progress(track_tqdm=Tru
|
|
| 60 |
file_name=uploaded.name,
|
| 61 |
config=import_cfg,
|
| 62 |
)
|
|
|
|
| 63 |
tick = 0
|
| 64 |
spinner = ["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"]
|
| 65 |
while not op.done:
|
|
@@ -72,7 +73,7 @@ def upload_and_index(client_state, file_obj, progress=gr.Progress(track_tqdm=Tru
|
|
| 72 |
yield store_name, f"Indexing **{fname}** …", _progress_html(overall, f"Indexing {fname}")
|
| 73 |
op = client.operations.get(op)
|
| 74 |
|
| 75 |
-
yield store_name, f"✅ File indexed into store `{store_name}`", _progress_html(100, "
|
| 76 |
|
| 77 |
def ask(client_state, store_name: str, history, question: str, model_id: str):
|
| 78 |
if client_state is None:
|
|
@@ -102,42 +103,58 @@ def ask(client_state, store_name: str, history, question: str, model_id: str):
|
|
| 102 |
|
| 103 |
return history, history
|
| 104 |
|
|
|
|
| 105 |
custom_css = """
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
"""
|
| 108 |
|
| 109 |
-
with gr.Blocks(
|
| 110 |
gr.HTML(f"<style>{custom_css}</style>")
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
client_state = gr.State(value=None)
|
| 113 |
store_state = gr.State(value="")
|
| 114 |
chat_state = gr.State(value=[])
|
| 115 |
|
| 116 |
-
with gr.Accordion("API Key (required)", open=True):
|
| 117 |
-
api_tb = gr.Textbox(label="Gemini API key", placeholder="Paste your API key…", type="password")
|
| 118 |
-
api_btn = gr.Button("Set API Key")
|
| 119 |
api_status = gr.Markdown()
|
| 120 |
|
| 121 |
-
with gr.
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
gr.Markdown("---")
|
| 127 |
-
gr.Markdown("### Ask questions about
|
| 128 |
-
question_tb = gr.Textbox(placeholder="Type
|
| 129 |
-
ask_btn = gr.Button("Ask")
|
| 130 |
chatbot = gr.Chatbot()
|
| 131 |
|
| 132 |
api_btn.click(ui_set_api_key, [api_tb], [client_state, api_status])
|
| 133 |
-
|
| 134 |
upload_btn.click(
|
| 135 |
upload_and_index,
|
| 136 |
[client_state, file_uploader],
|
| 137 |
[store_state, upload_status, upload_btn],
|
| 138 |
show_progress=True
|
| 139 |
)
|
| 140 |
-
|
| 141 |
ask_btn.click(
|
| 142 |
ask,
|
| 143 |
[client_state, store_state, chat_state, question_tb, gr.State(DEFAULT_MODEL)],
|
|
@@ -145,6 +162,4 @@ with gr.Blocks(title="Gemini File Search – Upload & Chat Demo") as demo:
|
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
-
demo.launch(
|
| 149 |
-
theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
|
| 150 |
-
)
|
|
|
|
| 29 |
return None, "❌ API key required."
|
| 30 |
try:
|
| 31 |
client = genai.Client(api_key=api_key)
|
| 32 |
+
return client, "✅ API key set — good to go."
|
| 33 |
except Exception as e:
|
| 34 |
return None, f"❌ Failed to set API key: {e}"
|
| 35 |
|
|
|
|
| 38 |
yield None, "❌ Set API key first.", _progress_html(0, "Waiting for API key")
|
| 39 |
return
|
| 40 |
if file_obj is None:
|
| 41 |
+
yield None, "⚠️ Please upload a file to index.", _progress_html(0, "Waiting")
|
| 42 |
return
|
| 43 |
|
| 44 |
client = _require_client(client_state)
|
|
|
|
| 60 |
file_name=uploaded.name,
|
| 61 |
config=import_cfg,
|
| 62 |
)
|
| 63 |
+
|
| 64 |
tick = 0
|
| 65 |
spinner = ["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"]
|
| 66 |
while not op.done:
|
|
|
|
| 73 |
yield store_name, f"Indexing **{fname}** …", _progress_html(overall, f"Indexing {fname}")
|
| 74 |
op = client.operations.get(op)
|
| 75 |
|
| 76 |
+
yield store_name, f"✅ File indexed into store `{store_name}`", _progress_html(100, "Completed!")
|
| 77 |
|
| 78 |
def ask(client_state, store_name: str, history, question: str, model_id: str):
|
| 79 |
if client_state is None:
|
|
|
|
| 103 |
|
| 104 |
return history, history
|
| 105 |
|
| 106 |
+
# Custom CSS for nicer look
|
| 107 |
custom_css = """
|
| 108 |
+
body {background-color: #f5f7fa;}
|
| 109 |
+
.gradio-container {max-width: 800px; margin: auto; padding-top: 20px;}
|
| 110 |
+
.header {text-align: center; margin-bottom: 30px;}
|
| 111 |
+
h1 {color: #4F46E5; margin-bottom: 5px;}
|
| 112 |
+
h2 {color: #334155;}
|
| 113 |
+
.progress-card { background: #fff; border: 1px solid #e5e7eb; border-radius: 8px; padding: 6px 10px; margin: 4px 0; }
|
| 114 |
+
.pbar {height: 8px; background: #e5e7eb; border-radius: 4px; overflow: hidden; margin: 6px 0;}
|
| 115 |
+
.pbar-fill {height: 100%; background: #4F46E5; transition: width .3s ease;}
|
| 116 |
+
.gr-box, .gr-panel { border-radius: 10px !important; }
|
| 117 |
+
.gr-button.primary { background-color: #4F46E5 !important; color: white !important; }
|
| 118 |
+
.gr-button { border-radius: 6px !important; }
|
| 119 |
+
.gr-file, .gr-textbox { border-radius: 6px !important; }
|
| 120 |
"""
|
| 121 |
|
| 122 |
+
with gr.Blocks() as demo:
|
| 123 |
gr.HTML(f"<style>{custom_css}</style>")
|
| 124 |
|
| 125 |
+
# Header
|
| 126 |
+
gr.Markdown("# 📄 Gemini File-Chat Demo", elem_classes="header")
|
| 127 |
+
gr.Markdown("Upload a document and ask questions — get grounded answers from the file.", elem_classes="header")
|
| 128 |
+
|
| 129 |
client_state = gr.State(value=None)
|
| 130 |
store_state = gr.State(value="")
|
| 131 |
chat_state = gr.State(value=[])
|
| 132 |
|
| 133 |
+
with gr.Accordion("🔑 API Key (required)", open=True):
|
| 134 |
+
api_tb = gr.Textbox(label="Gemini API key", placeholder="Paste your API key here…", type="password")
|
| 135 |
+
api_btn = gr.Button("Set API Key", elem_classes=["primary"])
|
| 136 |
api_status = gr.Markdown()
|
| 137 |
|
| 138 |
+
with gr.Box():
|
| 139 |
+
gr.Markdown("### 1) Upload & Index your file")
|
| 140 |
+
with gr.Row():
|
| 141 |
+
file_uploader = gr.File(label="Choose file to upload", file_types=['.txt', '.pdf', '.docx'])
|
| 142 |
+
upload_btn = gr.Button("Upload & Index", elem_classes=["primary"])
|
| 143 |
+
upload_status = gr.Markdown()
|
| 144 |
|
| 145 |
gr.Markdown("---")
|
| 146 |
+
gr.Markdown("### 2) Ask questions about your file")
|
| 147 |
+
question_tb = gr.Textbox(label="Your question", placeholder="Type a question…")
|
| 148 |
+
ask_btn = gr.Button("Ask", elem_classes=["primary"])
|
| 149 |
chatbot = gr.Chatbot()
|
| 150 |
|
| 151 |
api_btn.click(ui_set_api_key, [api_tb], [client_state, api_status])
|
|
|
|
| 152 |
upload_btn.click(
|
| 153 |
upload_and_index,
|
| 154 |
[client_state, file_uploader],
|
| 155 |
[store_state, upload_status, upload_btn],
|
| 156 |
show_progress=True
|
| 157 |
)
|
|
|
|
| 158 |
ask_btn.click(
|
| 159 |
ask,
|
| 160 |
[client_state, store_state, chat_state, question_tb, gr.State(DEFAULT_MODEL)],
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
| 165 |
+
demo.launch(theme=gr.themes.Soft())
|
|
|
|
|
|