ShubhamMhaske commited on
Commit
f08be97
·
verified ·
1 Parent(s): 46b5770

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -15,13 +15,13 @@ def _require_client(client_obj):
15
 
16
  def _progress_html(pct: float, text: str) -> str:
17
  pct = max(0, min(100, pct))
18
- return f\"\"\"
19
- <div class="progress-card">
20
- <div class="progress-head">{text}</div>
21
- <div class="pbar"><div class="pbar-fill" style="width:{pct:.0f}%"></div></div>
22
- <div class="pbar-foot">{pct:.0f}%</div>
23
- </div>
24
- \"\"\"
25
 
26
  def ui_set_api_key(api_key: str):
27
  api_key = (api_key or "").strip()
@@ -33,8 +33,7 @@ def ui_set_api_key(api_key: str):
33
  except Exception as e:
34
  return None, f"❌ Failed to set API key: {e}"
35
 
36
- def upload_and_index(client_state, file_obj,
37
- progress=gr.Progress(track_tqdm=True)):
38
  if client_state is None:
39
  yield None, "❌ Set API key first.", _progress_html(0, "Waiting for API key")
40
  return
@@ -44,7 +43,8 @@ def upload_and_index(client_state, file_obj,
44
 
45
  client = _require_client(client_state)
46
 
47
- store = client.file_search_stores.create(config={"display_name": "my-upload-store"})
 
48
  store_name = store.name
49
 
50
  fname = Path(file_obj.name).name
@@ -74,16 +74,16 @@ def upload_and_index(client_state, file_obj,
74
 
75
  yield store_name, f"✅ File indexed into store `{store_name}`", _progress_html(100, "Done")
76
 
77
- def ask(client_state, store_name: str, history_msgs, question: str, model_id: str):
78
  if client_state is None:
79
- return history_msgs, "❌ Set API key first."
80
  if not store_name:
81
- return history_msgs, "⚠️ Upload & index a file first."
82
-
83
- client = _require_client(client_state)
84
  q = (question or "").strip()
85
  if not q:
86
- return history_msgs, "⚠️ Please type a question."
 
 
87
 
88
  tool = types.Tool(
89
  file_search=types.FileSearch(
@@ -96,13 +96,15 @@ def ask(client_state, store_name: str, history_msgs, question: str, model_id: st
96
  config=types.GenerateContentConfig(tools=[tool]),
97
  )
98
  answer = resp.text or "No answer."
99
- history = list(history_msgs or [])
100
  history.append({"role": "user", "content": q})
101
  history.append({"role": "assistant", "content": answer})
102
 
103
- return history, history # return history as both chat history & output
104
 
105
- custom_css = \"\"\"/* Optional CSS here */\"\"\"
 
 
106
 
107
  with gr.Blocks(title="Gemini File Search – Upload & Chat Demo") as demo:
108
  gr.HTML(f"<style>{custom_css}</style>")
@@ -125,7 +127,7 @@ with gr.Blocks(title="Gemini File Search – Upload & Chat Demo") as demo:
125
  gr.Markdown("### Ask questions about the uploaded file")
126
  question_tb = gr.Textbox(placeholder="Type your question…", show_label=False)
127
  ask_btn = gr.Button("Ask")
128
- chatbot = gr.Chatbot() # No `type=` argument
129
 
130
  api_btn.click(ui_set_api_key, [api_tb], [client_state, api_status])
131
 
 
15
 
16
  def _progress_html(pct: float, text: str) -> str:
17
  pct = max(0, min(100, pct))
18
+ return (
19
+ "<div class=\"progress-card\">"
20
+ f"<div class=\"progress-head\">{text}</div>"
21
+ f"<div class=\"pbar\"><div class=\"pbar-fill\" style=\"width:{pct:.0f}%\"></div></div>"
22
+ f"<div class=\"pbar-foot\">{pct:.0f}%</div>"
23
+ "</div>"
24
+ )
25
 
26
  def ui_set_api_key(api_key: str):
27
  api_key = (api_key or "").strip()
 
33
  except Exception as e:
34
  return None, f"❌ Failed to set API key: {e}"
35
 
36
+ def upload_and_index(client_state, file_obj, progress=gr.Progress(track_tqdm=True)):
 
37
  if client_state is None:
38
  yield None, "❌ Set API key first.", _progress_html(0, "Waiting for API key")
39
  return
 
43
 
44
  client = _require_client(client_state)
45
 
46
+ # Create a new store
47
+ store = client.file_search_stores.create(config={"display_name": "uploaded-store"})
48
  store_name = store.name
49
 
50
  fname = Path(file_obj.name).name
 
74
 
75
  yield store_name, f"✅ File indexed into store `{store_name}`", _progress_html(100, "Done")
76
 
77
+ def ask(client_state, store_name: str, history, question: str, model_id: str):
78
  if client_state is None:
79
+ return history, "❌ Set API key first."
80
  if not store_name:
81
+ return history, "⚠️ Upload & index a file first."
 
 
82
  q = (question or "").strip()
83
  if not q:
84
+ return history, "⚠️ Please type a question."
85
+
86
+ client = _require_client(client_state)
87
 
88
  tool = types.Tool(
89
  file_search=types.FileSearch(
 
96
  config=types.GenerateContentConfig(tools=[tool]),
97
  )
98
  answer = resp.text or "No answer."
99
+ history = history or []
100
  history.append({"role": "user", "content": q})
101
  history.append({"role": "assistant", "content": answer})
102
 
103
+ return history, history
104
 
105
+ custom_css = """
106
+ /* Optional: add your custom CSS here */
107
+ """
108
 
109
  with gr.Blocks(title="Gemini File Search – Upload & Chat Demo") as demo:
110
  gr.HTML(f"<style>{custom_css}</style>")
 
127
  gr.Markdown("### Ask questions about the uploaded file")
128
  question_tb = gr.Textbox(placeholder="Type your question…", show_label=False)
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