abdibrokhim commited on
Commit
5058660
·
1 Parent(s): 21ffffb

search for images, videos, links, and follow up questions working nice

Browse files
Files changed (8) hide show
  1. .gitignore +0 -1
  2. app.py +59 -29
  3. helpers.py +83 -64
  4. prompts-followup.md +1453 -0
  5. prompts.py +21 -1
  6. requirements.txt +2 -1
  7. test_api.py +30 -16
  8. test_app.py +0 -41
.gitignore CHANGED
@@ -1,4 +1,3 @@
1
- prompts.md
2
  .env
3
  .venv
4
  __pycache__
 
 
1
  .env
2
  .venv
3
  __pycache__
app.py CHANGED
@@ -2,10 +2,14 @@ import os
2
  import gradio as gr
3
  from bagoodex_client import BagoodexClient
4
  from r_types import ChatMessage
5
- from prompts import SYSTEM_PROMPT_FOLLOWUP, SYSTEM_PROMPT_MAP, SYSTEM_PROMPT_BASE
 
 
 
 
 
6
  from helpers import (
7
  embed_video,
8
- embed_image,
9
  format_links,
10
  embed_google_map,
11
  format_knowledge,
@@ -64,18 +68,22 @@ def handle_local_map_click(followup_state, chat_history_state):
64
  return chat_history_state
65
  try:
66
  result = client.get_local_map(followup_state)
67
- map_url = result.get('link', '')
68
- # Use helper to produce an embedded map iframe
69
- html = embed_google_map(map_url)
 
 
 
 
 
 
 
 
 
 
 
70
  except Exception:
71
- # Fall back: use the base_qna call with SYSTEM_PROMPT_MAP
72
- result = client.base_qna(
73
- messages=chat_history_state, system_prompt=SYSTEM_PROMPT_MAP
74
- )
75
- # Assume result contains a 'link' field
76
- html = embed_google_map(result.get('link', ''))
77
- new_message = ChatMessage({"role": "assistant", "content": html})
78
- return chat_history_state + [new_message]
79
 
80
  def handle_knowledge_click(followup_state, chat_history_state):
81
  """
@@ -83,10 +91,23 @@ def handle_knowledge_click(followup_state, chat_history_state):
83
  """
84
  if not followup_state:
85
  return chat_history_state
86
- result = client.get_knowledge(followup_state)
87
- md = format_knowledge(result)
88
- new_message = ChatMessage({"role": "assistant", "content": md})
89
- return chat_history_state + [new_message]
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # ----------------------------
92
  # Advanced Search Functions
@@ -120,6 +141,9 @@ css = """
120
  }
121
  """
122
 
 
 
 
123
  with gr.Blocks(css=css, fill_height=True) as demo:
124
  # State variables to hold followup ID and conversation history, plus follow-up questions text
125
  followup_state = gr.State(None)
@@ -129,8 +153,8 @@ with gr.Blocks(css=css, fill_height=True) as demo:
129
  with gr.Row():
130
  with gr.Column(scale=3):
131
  with gr.Row():
132
- btn_local_map = gr.Button("Local Map Search", variant="secondary", size="sm")
133
- btn_knowledge = gr.Button("Knowledge Base", variant="secondary", size="sm")
134
  # The ChatInterface now uses additional outputs for both followup_state and conversation history,
135
  # plus follow-up questions Markdown.
136
  chat = gr.ChatInterface(
@@ -150,34 +174,40 @@ with gr.Blocks(css=css, fill_height=True) as demo:
150
  inputs=[followup_state, chat_history_state],
151
  outputs=chat.chatbot
152
  )
153
- # Below the chat input, display follow-up questions and let user select one.
 
154
  followup_radio = gr.Radio(
155
- choices=[], label="Follow-up Questions (select one and click Send Follow-up)"
 
156
  )
157
  btn_send_followup = gr.Button("Send Follow-up")
158
- # When a follow-up question is sent, update the chat conversation, followup state, and follow-up list.
 
 
159
  btn_send_followup.click(
160
  fn=handle_followup_click,
161
  inputs=[followup_radio, followup_state, chat_history_state],
162
  outputs=[chat.chatbot, followup_state, followup_md_state]
163
  )
164
- # Also display the follow-up questions markdown (for reference) in a Markdown component.
165
- followup_markdown = gr.Markdown(label="Follow-up Questions", value="", visible=True)
166
- # When the followup_md_state updates, also update the radio choices.
167
  def update_followup_radio(md_text):
168
- # Assume the helper output is a Markdown string with list items.
169
- # We split the text to extract the question lines.
 
170
  lines = md_text.splitlines()
171
  questions = []
172
  for line in lines:
173
  if line.startswith("- "):
174
  questions.append(line[2:])
175
- return gr.update(choices=questions, value=None), md_text
 
176
  followup_md_state.change(
177
  fn=update_followup_radio,
178
  inputs=[followup_md_state],
179
- outputs=[followup_radio, followup_markdown]
180
  )
 
181
  with gr.Column(scale=1):
182
  gr.Markdown("### Advanced Search Options")
183
  with gr.Column(variant="panel"):
 
2
  import gradio as gr
3
  from bagoodex_client import BagoodexClient
4
  from r_types import ChatMessage
5
+ from prompts import (
6
+ SYSTEM_PROMPT_FOLLOWUP,
7
+ SYSTEM_PROMPT_MAP,
8
+ SYSTEM_PROMPT_BASE,
9
+ SYSTEM_PROMPT_KNOWLEDGE_BASE
10
+ )
11
  from helpers import (
12
  embed_video,
 
13
  format_links,
14
  embed_google_map,
15
  format_knowledge,
 
68
  return chat_history_state
69
  try:
70
  result = client.get_local_map(followup_state)
71
+
72
+ if result:
73
+ map_url = result.get('link', '')
74
+ # Use helper to produce an embedded map iframe
75
+ html = embed_google_map(map_url)
76
+
77
+ # Fall back: use the base_qna call with SYSTEM_PROMPT_MAP
78
+ result = client.base_qna(
79
+ messages=chat_history_state, system_prompt=SYSTEM_PROMPT_MAP
80
+ )
81
+ # Assume result contains a 'link' field
82
+ html = embed_google_map(result.get('link', ''))
83
+ new_message = ChatMessage({"role": "assistant", "content": html})
84
+ return chat_history_state + [new_message]
85
  except Exception:
86
+ return chat_history_state
 
 
 
 
 
 
 
87
 
88
  def handle_knowledge_click(followup_state, chat_history_state):
89
  """
 
91
  """
92
  if not followup_state:
93
  return chat_history_state
94
+
95
+ try:
96
+ print('trying to get knowledge')
97
+ result = client.get_knowledge(followup_state)
98
+ knowledge_md = format_knowledge(result)
99
+
100
+ if knowledge_md == 0000:
101
+ print('falling back to base_qna')
102
+ # Fall back: use the base_qna call with SYSTEM_PROMPT_KNOWLEDGE_BASE
103
+ result = client.base_qna(
104
+ messages=chat_history_state, system_prompt=SYSTEM_PROMPT_KNOWLEDGE_BASE
105
+ )
106
+ knowledge_md = format_knowledge(result)
107
+ new_message = ChatMessage({"role": "assistant", "content": knowledge_md})
108
+ return chat_history_state + [new_message]
109
+ except Exception:
110
+ return chat_history_state
111
 
112
  # ----------------------------
113
  # Advanced Search Functions
 
141
  }
142
  """
143
 
144
+
145
+ # defautl query: how to make slingshot?
146
+ # who created light (e.g., electricity) Tesla or Edison in quick short?
147
  with gr.Blocks(css=css, fill_height=True) as demo:
148
  # State variables to hold followup ID and conversation history, plus follow-up questions text
149
  followup_state = gr.State(None)
 
153
  with gr.Row():
154
  with gr.Column(scale=3):
155
  with gr.Row():
156
+ btn_local_map = gr.Button("Local Map Search (coming soon...)", variant="secondary", size="sm", interactive=False)
157
+ btn_knowledge = gr.Button("Knowledge Base (coming soon...)", variant="secondary", size="sm", interactive=False)
158
  # The ChatInterface now uses additional outputs for both followup_state and conversation history,
159
  # plus follow-up questions Markdown.
160
  chat = gr.ChatInterface(
 
174
  inputs=[followup_state, chat_history_state],
175
  outputs=chat.chatbot
176
  )
177
+
178
+ # Radio-based follow-up questions
179
  followup_radio = gr.Radio(
180
+ choices=[],
181
+ label="Follow-up Questions (select one and click 'Send Follow-up')"
182
  )
183
  btn_send_followup = gr.Button("Send Follow-up")
184
+
185
+ # When the user clicks "Send Follow-up", the selected question is passed
186
+ # to handle_followup_click
187
  btn_send_followup.click(
188
  fn=handle_followup_click,
189
  inputs=[followup_radio, followup_state, chat_history_state],
190
  outputs=[chat.chatbot, followup_state, followup_md_state]
191
  )
192
+
193
+ # Update the radio choices when followup_md_state changes
 
194
  def update_followup_radio(md_text):
195
+ """
196
+ Parse Markdown lines to extract questions starting with '- '.
197
+ """
198
  lines = md_text.splitlines()
199
  questions = []
200
  for line in lines:
201
  if line.startswith("- "):
202
  questions.append(line[2:])
203
+ return gr.update(choices=questions, value=None)
204
+
205
  followup_md_state.change(
206
  fn=update_followup_radio,
207
  inputs=[followup_md_state],
208
+ outputs=[followup_radio]
209
  )
210
+
211
  with gr.Column(scale=1):
212
  gr.Markdown("### Advanced Search Options")
213
  with gr.Column(variant="panel"):
helpers.py CHANGED
@@ -4,7 +4,7 @@ import gradio as gr
4
  import urllib.parse
5
  import re
6
  from pytube import YouTube
7
- from typing import List, Optional
8
  from r_types import (
9
  SearchVideosResponse,
10
  SearchImagesResponse,
@@ -12,6 +12,7 @@ from r_types import (
12
  LocalMapResponse,
13
  KnowledgeBaseResponse
14
  )
 
15
 
16
 
17
  def get_video_id(url: str) -> Optional[str]:
@@ -68,45 +69,10 @@ def embed_video(videos: List[SearchVideosResponse]) -> str:
68
  # Join all iframes into one HTML string
69
  return "\n".join(iframes)
70
 
 
 
71
 
72
- def embed_image(json_data: SearchImagesResponse) -> str:
73
- """
74
- Given image data with 'original' (URL) and 'title',
75
- returns an HTML string with an <img> tag.
76
- """
77
- title = json_data.get("title", "").replace('"', '\\"')
78
- original = json_data.get("original", "")
79
-
80
- if not original:
81
- return "<p>No image URL provided.</p>"
82
-
83
- embed_html = f"""
84
- <img src="{original}" alt="{title}" style="width:100%">
85
- """
86
- return embed_html
87
-
88
-
89
- def build_search_links_response(urls: List[str]) -> List[SearchLinksResponse]:
90
- """
91
- Convert raw URLs into a list of dicts,
92
- each with 'title' and 'link' keys for display.
93
- """
94
- results = []
95
- for url in urls:
96
- # Extract the last part of the URL as a rough "title"
97
- raw_title = url.rstrip("/").split("/")[-1]
98
-
99
- # Decode URL-encoded entities like %20
100
- decoded_title = urllib.parse.unquote(raw_title)
101
-
102
- # Replace hyphens/underscores with spaces
103
- nice_title = decoded_title.replace("_", " ").replace("-", " ")
104
-
105
- results.append({"title": nice_title, "link": url})
106
- return results
107
-
108
-
109
- def format_links(links: List[SearchLinksResponse]) -> str:
110
  """
111
  Convert a list of {'title': str, 'link': str} objects
112
  into a bulleted Markdown string with clickable links.
@@ -114,9 +80,10 @@ def format_links(links: List[SearchLinksResponse]) -> str:
114
  if not links:
115
  return "No links found."
116
 
117
- links_md = "### Links\n\n"
118
- for item in links:
119
- links_md += f"- [{item['title']}]({item['link']})\n"
 
120
  return links_md
121
 
122
 
@@ -157,34 +124,86 @@ def embed_google_map(map_url: str) -> str:
157
  return embed_html
158
 
159
 
160
- def format_knowledge(result: KnowledgeBaseResponse) -> str:
161
  """
162
  Given a dictionary of knowledge data (e.g., about a person),
163
  produce a Markdown string summarizing that info.
164
  """
165
- title = result.get("title", "Unknown")
166
- type_ = result.get("type", "")
167
- born = result.get("born", "")
168
- died = result.get("died", "")
169
-
170
- content = f"""
171
- **{title}**
172
- Type: {type_}
173
- Born: {born}
174
- Died: {died}
175
- """
176
- return content
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
 
179
- def format_followup_questions(questions: List[str]) -> str:
 
180
  """
181
- Given a list of follow-up questions, return a Markdown string
182
- with each question as a bulleted list item.
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  """
184
- if not questions:
185
- return "No follow-up questions provided."
186
 
187
- questions_md = "### Follow-up Questions\n\n"
188
- for question in questions:
189
- questions_md += f"- {question}\n"
190
- return questions_md
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import urllib.parse
5
  import re
6
  from pytube import YouTube
7
+ from typing import List, Optional, Dict
8
  from r_types import (
9
  SearchVideosResponse,
10
  SearchImagesResponse,
 
12
  LocalMapResponse,
13
  KnowledgeBaseResponse
14
  )
15
+ import json
16
 
17
 
18
  def get_video_id(url: str) -> Optional[str]:
 
69
  # Join all iframes into one HTML string
70
  return "\n".join(iframes)
71
 
72
+ def get_video_thumbnail(videos: List[SearchVideosResponse]) -> str:
73
+ pass
74
 
75
+ def format_links(links) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  """
77
  Convert a list of {'title': str, 'link': str} objects
78
  into a bulleted Markdown string with clickable links.
 
80
  if not links:
81
  return "No links found."
82
 
83
+ links_md = "**Links:**\n"
84
+ for url in links:
85
+ title = url.rstrip('/').split('/')[-1]
86
+ links_md += f"- [{title}]({url})\n"
87
  return links_md
88
 
89
 
 
124
  return embed_html
125
 
126
 
127
+ def format_knowledge(raw_result: str) -> str:
128
  """
129
  Given a dictionary of knowledge data (e.g., about a person),
130
  produce a Markdown string summarizing that info.
131
  """
132
+
133
+ if not raw_result:
134
+ return 0000
135
+
136
+ # Clean up the raw JSON string
137
+ clean_json_str = cleanup_raw_json(raw_result)
138
+ print('Knowledge Data: ', clean_json_str)
139
+
140
+ try:
141
+ # Parse the cleaned JSON string
142
+ result = json.loads(clean_json_str)
143
+ title = result.get("title", "...")
144
+ type_ = result.get("type", "...")
145
+ born = result.get("born", "...")
146
+ died = result.get("died", "...")
147
+
148
+ content = f"""
149
+ **{title}**
150
+ Type: {type_}
151
+ Born: {born}
152
+ Died: {died}
153
+ """
154
+ return content
155
+ except json.JSONDecodeError:
156
+ return "Error: Failed to parse knowledge data."
157
 
158
 
159
+
160
+ def format_followup_questions(raw_questions: str) -> str:
161
  """
162
+ Extracts and formats follow-up questions from a raw JSON-like string.
163
+
164
+ The input string may contain triple backticks (```json ... ```) which need to be removed before parsing.
165
+
166
+ Expected input format:
167
+ ```json
168
+ {
169
+ "followup_question": [
170
+ "What materials are needed to make a slingshot?",
171
+ "How to make a slingshot more powerful?"
172
+ ]
173
+ }
174
+ ```
175
+
176
+ Returns a Markdown-formatted string with the follow-up questions.
177
  """
 
 
178
 
179
+ if not raw_questions:
180
+ return "No follow-up questions available."
181
+
182
+ # Clean up the raw JSON string
183
+ clean_json_str = cleanup_raw_json(raw_questions)
184
+
185
+ try:
186
+ # Parse the cleaned JSON string
187
+ questions_dict = json.loads(clean_json_str)
188
+
189
+ # Ensure the expected key exists
190
+ followup_list = questions_dict.get("followup_question", [])
191
+
192
+ if not isinstance(followup_list, list) or not followup_list:
193
+ return "No follow-up questions available."
194
+
195
+ # Format the questions into Markdown
196
+ questions_md = "### Follow-up Questions\n\n"
197
+ for question in followup_list:
198
+ questions_md += f"- {question}\n"
199
+
200
+ return questions_md
201
+
202
+ except json.JSONDecodeError:
203
+ return "Error: Failed to parse follow-up questions."
204
+
205
+ def cleanup_raw_json(raw_json: str) -> str:
206
+ """
207
+ Remove triple backticks and 'json' from the beginning and end of a raw JSON string.
208
+ """
209
+ return re.sub(r"```json|```", "", raw_json).strip()
prompts-followup.md ADDED
@@ -0,0 +1,1453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [initial_prompt]:
2
+
3
+ Here is the website on: "How to Create a Chatbot with Gradio" -> "https://www.gradio.app/guides/creating-a-chatbot-fast". I believe you can scrape and learn on the go. So could you tell me what tutorial is <next page> (just checking). If you succeed to answer we will proceed with you. I have great proposal to you.
4
+
5
+ [follow_up]:
6
+
7
+ Great. Thats correct!!
8
+ Please take a deep breath.
9
+ Now, let's proceed with the building real world Gradio app.
10
+ I will guide you through the process. Let's start with the first step.
11
+
12
+ # UI/UX for Bagoodex Search API
13
+
14
+ ## Few things to note:
15
+
16
+ 0. UI is just a simple chatbot interface by default. We can devide into two parts. Left side for the chat (e.g., ChatGPT UI) and right side for the Advanced Search options. (It's like Perplexity UI. in any case refer to "https://www.perplexity.ai/".).
17
+ 1. We will be using Gradio to create a simple UI for the Bagoodex Search API.
18
+ 2. The API delivers real-time AI-powered web search with NLP capabilities.
19
+ 3. The API can be used to search for links, images, videos, local maps, and knowledge about a topic.
20
+ 4. Our Gradio app should be configurable from user side. (refer to [advanced search syntax](#advanced-search-syntax)).
21
+
22
+ ## Requirements:
23
+
24
+ 0. As you will see below output is already known. We need to create classes for each type of search. It makes the code more readable and maintainable.
25
+ 1. When user enters a query, the defautl chat API endpoint should return the results based on the query.
26
+ 2. On the right side list the advanced search options (e.g., images, videos).
27
+ For example (in NextJS):
28
+ <div className="flex flex-col gap-2">
29
+ <div className="border border-gray-300 px-3 py-2 rounded-md flex items-center justify-between">
30
+ <p>Search Images</p>
31
+ <button className="bg-blue-500 text-white px-2 py-1 rounded-md">[plus icon that sends request]</button>
32
+ </div>
33
+ <div className="border border-gray-300 px-3 py-2 rounded-md flex items-center justify-between">
34
+ <p>Search Videos</p>
35
+ <button className="bg-blue-500 text-white px-2 py-1 rounded-md">[plus icon that sends request]</button>
36
+ </div>
37
+ </div>
38
+
39
+ (It's like Perplexity UI/UX.).
40
+
41
+ 3. On input field we should add several buttons for the rest of the advanced search options.
42
+ For example:
43
+ 1) user can click on "local maps" and activate it. So in addition to the results we should display and render the map using Gradio specific components.
44
+ 2) user can click on "knowledge about a topic" that will return a structured knowledge base about the topic. If user wants fast and structured information.
45
+
46
+ ## Addition:
47
+ 1. Create several files and helper functions as needed.
48
+ 2. Use the provided code snippets to build the app.
49
+ 3. I am not pushing you to generate all the files and codebase in one shot. You may ask follow up questions and generate rest of the codebase/ files/ functions for Gradio app.
50
+
51
+ # API request examples:
52
+
53
+ > model=`bagoodex/bagoodex-search-v1`.
54
+
55
+ ## 1. As a regular chat completion model (but searching on the internet):
56
+
57
+ Get API Key from `.env` file:
58
+ <code_snippet>
59
+ ```py
60
+ import os
61
+ from dotenv import load_dotenv
62
+ load_dotenv()
63
+ AIML_API_KEY = os.getenv('AIML_API_KEY')
64
+ ```
65
+ </code_snippet>
66
+ -----
67
+ <code_snippet>
68
+ ```py
69
+ import requests
70
+ from openai import OpenAI
71
+
72
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
73
+ API_KEY = '<YOUR_API_KEY>'
74
+ API_URL = 'https://api.aimlapi.com'
75
+
76
+ # Call the standart chat completion endpoint to get an ID
77
+ def complete_chat():
78
+ client = OpenAI(
79
+ base_url=API_URL,
80
+ api_key=API_KEY,
81
+ )
82
+
83
+ response = client.chat.completions.create(
84
+ model="bagoodex/bagoodex-search-v1",
85
+ messages=[
86
+ {
87
+ "role": "user",
88
+
89
+ # Enter your query here
90
+ "content": 'how to make a slingshot',
91
+ },
92
+ ],
93
+ )
94
+
95
+
96
+ print(response.choices[0].message.content)
97
+
98
+
99
+ # Run the function
100
+ complete_chat()
101
+ ```
102
+ </code_snippet>
103
+
104
+ ### Model Response:
105
+
106
+ <response>
107
+ ```
108
+ To make a slingshot, you can follow the instructions provided in the two sources:
109
+
110
+ **Option 1: Make a Giant Slingshot**
111
+
112
+ * Start by cutting two 2x4's to a length of 40 inches each, which will be the main arms of the slingshot.
113
+ * Attach the arms to a base made of plywood using screws, and then add side braces to support the arms.
114
+ * Install an exercise band as the launching mechanism, making sure to tighten it to achieve the desired distance.
115
+ * Add a cross brace to keep the arms rigid and prevent them from spreading or caving in.
116
+
117
+ **Option 2: Make a Stick Slingshot**
118
+
119
+ * Find a sturdy, Y-shaped stick and break it down to the desired shape.
120
+ * Cut notches on the ends of the stick to hold the rubber bands in place.
121
+ * Create a pouch by folding a piece of fabric in half and then half again, and then cutting small holes for the rubber bands.
122
+ * Thread the rubber bands through the holes and tie them securely to the stick using thread.
123
+ * Decorate the slingshot with coloured yarn or twine if desired.
124
+
125
+ You can choose to make either a giant slingshot or a stick slingshot, depending on your preference and the materials available.
126
+ ```
127
+ </response>
128
+
129
+ ----
130
+
131
+ ## 2. Using six specialized API endpoints, each designed to search for only one specific type of information:
132
+ <use_cases>
133
+ [1]. Links -> refer to [Find Links](#1-find-links)
134
+ [2]. Images -> refer to [Find Images](#2-find-images)
135
+ [3]. Videos -> refer to [Find Videos](#3-find-videos)
136
+ [4]. Locations -> refer to [Find a Local Map](#4-find-a-local-map)
137
+ [5]. Knowledge about a topic, structured as a small knowledge base -> refer to [Knowledge about a topic](#5-knowledge-about-a-topic-structured-as-a-small-knowledge-base)
138
+ </use_cases>
139
+
140
+ #### Advanced search syntax
141
+ Note that queries can include advanced search syntax:
142
+ <note>
143
+ 1. Search for an exact match: Enter a word or phrase using \" before and after it.
144
+ For example, \"tallest building\".
145
+ 2. Search for a specific site: Enter site: in front of a site or domain.
146
+ For example, site:youtube.com cat videos.
147
+ 3. Exclude words from your search: Enter - in front of a word that you want to leave out.
148
+ For example, jaguar speed -car.
149
+ </note>
150
+
151
+ ----
152
+
153
+ ## 1. Find Links
154
+
155
+ <important>
156
+ First, you must first call the standard chat completion endpoint with your query.
157
+ The chat completion endpoint returns an ID, which must then be passed as the sole input parameter followup_id to the bagoodex/links endpoint below.
158
+ </important>
159
+
160
+ ### Example:
161
+ <code_snippet>
162
+ ```py
163
+ import requests
164
+ from openai import OpenAI
165
+
166
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
167
+ API_KEY = '<YOUR_API_KEY>'
168
+ API_URL = 'https://api.aimlapi.com'
169
+
170
+ # Call the standart chat completion endpoint to get an ID
171
+ def complete_chat():
172
+ client = OpenAI(
173
+ base_url=API_URL,
174
+ api_key=API_KEY,
175
+ )
176
+
177
+ response = client.chat.completions.create(
178
+ model="bagoodex/bagoodex-search-v1",
179
+ messages=[
180
+ {
181
+ "role": "user",
182
+ "content": "site:www.reddit.com AI",
183
+ },
184
+ ],
185
+ )
186
+
187
+ # Extract the ID from the response
188
+ gen_id = response.id
189
+ print(f"Generated ID: {gen_id}")
190
+
191
+ # Call the Bagoodex endpoint with the generated ID
192
+ get_links(gen_id)
193
+
194
+ def get_links(gen_id):
195
+ params = {'followup_id': gen_id}
196
+ headers = {'Authorization': f'Bearer {API_KEY}'}
197
+ response = requests.get(f'{API_URL}/v1/bagoodex/links', headers=headers, params=params)
198
+
199
+ print(response.json())
200
+
201
+ # Run the function
202
+ complete_chat()
203
+ ```
204
+ </code_snippet>
205
+
206
+ ### Model Response:
207
+ <response>
208
+ ```
209
+ [
210
+ "https://www.reddit.com/r/artificial/",
211
+ "https://www.reddit.com/r/ArtificialInteligence/",
212
+ "https://www.reddit.com/r/artificial/wiki/getting-started/",
213
+ "https://www.reddit.com/r/ChatGPT/comments/1fwt2zf/it_is_officially_over_these_are_all_ai/",
214
+ "https://www.reddit.com/r/ArtificialInteligence/comments/1f8wxe7/whats_the_most_surprising_way_ai_has_become_part/",
215
+ "https://gist.github.com/nndda/a985daed53283a2c7fd399e11a185b11",
216
+ "https://www.reddit.com/r/aivideo/",
217
+ "https://www.reddit.com/r/singularity/",
218
+ "https://www.abc.net.au/",
219
+ "https://www.reddit.com/r/PromptEngineering/"
220
+ ]
221
+ ```
222
+ </response>
223
+
224
+ ## 2. Find Images
225
+
226
+ <important>
227
+ First, you must first call the standard chat completion endpoint with your query.
228
+ The chat completion endpoint returns an ID, which must then be passed as the sole input parameter followup_id to the bagoodex/images endpoint below.
229
+ </important>
230
+
231
+ ### Example:
232
+ <code_snippet>
233
+ ```py
234
+ import requests
235
+ from openai import OpenAI
236
+
237
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
238
+ API_KEY = '<YOUR_API_KEY>'
239
+ API_URL = 'https://api.aimlapi.com'
240
+
241
+ # Call the standart chat completion endpoint to get an ID
242
+ def complete_chat():
243
+ client = OpenAI(
244
+ base_url=API_URL,
245
+ api_key=API_KEY,
246
+ )
247
+
248
+ response = client.chat.completions.create(
249
+ model="bagoodex/bagoodex-search-v1",
250
+ messages=[
251
+ {
252
+ "role": "user",
253
+ "content": "giant dragonflies",
254
+ },
255
+ ],
256
+ )
257
+
258
+ # Extract the ID from the response
259
+ gen_id = response.id
260
+ print(f"Generated ID: {gen_id}")
261
+
262
+ # Call the Bagoodex endpoint with the generated ID
263
+ get_images(gen_id)
264
+
265
+ def get_images(gen_id):
266
+ params = {'followup_id': gen_id}
267
+ headers = {'Authorization': f'Bearer {API_KEY}'}
268
+ response = requests.get(f'{API_URL}/v1/bagoodex/images', headers=headers, params=params)
269
+
270
+ print(response.json())
271
+
272
+ # Run the function
273
+ complete_chat()
274
+ ```
275
+ </code_snippet>
276
+
277
+ ### Model Response:
278
+ <response>
279
+ ```
280
+ [
281
+ {
282
+ "source": "",
283
+ "original": "https://images.theconversation.com/files/234118/original/file-20180829-195319-1d4y13t.jpg?ixlib=rb-4.1.0&rect=0%2C7%2C1200%2C790&q=45&auto=format&w=926&fit=clip",
284
+ "title": "Paleozoic era's giant dragonflies ...",
285
+ "source_name": "The Conversation"
286
+ },
287
+ {
288
+ "source": "",
289
+ "original": "https://s3-us-west-1.amazonaws.com/scifindr/articles/image3s/000/002/727/large/meganeuropsis-eating-roach_lucas-lima_3x4.jpg?1470033295",
290
+ "title": "huge dragonfly ...",
291
+ "source_name": "Earth Archives"
292
+ },
293
+ {
294
+ "source": "",
295
+ "original": "https://s3-us-west-1.amazonaws.com/scifindr/articles/image2s/000/002/727/large/meganeuropsis_lucas-lima_4x3.jpg?1470033293",
296
+ "title": "huge dragonfly ...",
297
+ "source_name": "Earth Archives"
298
+ },
299
+ {
300
+ "source": "",
301
+ "original": "https://static.wikia.nocookie.net/prehistoricparkip/images/3/37/Meganeurid_bbc_prehistoric_.jpg/revision/latest?cb=20120906182204",
302
+ "title": "Giant Dragonfly | Prehistoric Park Wiki ...",
303
+ "source_name": "Prehistoric Park Wiki - Fandom"
304
+ },
305
+ {
306
+ "source": "",
307
+ "original": "https://i.redd.it/rig989kttmc71.jpg",
308
+ "title": "This pretty large dragonfly we found ...",
309
+ "source_name": "Reddit"
310
+ },
311
+ {
312
+ "source": "",
313
+ "original": "https://upload.wikimedia.org/wikipedia/commons/f/fc/Meganeurites_gracilipes_restoration.webp",
314
+ "title": "Meganisoptera - Wikipedia",
315
+ "source_name": "Wikipedia"
316
+ },
317
+ {
318
+ "source": "",
319
+ "original": "https://sites.wustl.edu/monh/files/2019/12/woman-and-meganeura-350x263.jpeg",
320
+ "title": "Dragonflies and Damselflies of Missouri ...",
321
+ "source_name": "Washington University"
322
+ },
323
+ {
324
+ "source": "",
325
+ "original": "http://www.stancsmith.com/uploads/4/8/9/6/48964465/meganeuropsis-giantdragonfly_orig.jpg",
326
+ "title": "Ginormous Dragonfly - Stan C ...",
327
+ "source_name": "Stan C. Smith"
328
+ },
329
+ {
330
+ "source": "",
331
+ "original": "https://static.sciencelearn.org.nz/images/images/000/004/172/original/INSECTS_ITV_Image_map_Aquatic_insects_Dragonfly.jpg?1674173331",
332
+ "title": "Bush giant dragonfly — Science ...",
333
+ "source_name": "Science Learning Hub"
334
+ },
335
+ {
336
+ "source": "",
337
+ "original": "https://i.ytimg.com/vi/ixlQX7lV8dc/sddefault.jpg",
338
+ "title": "Meganeura' - The Prehistoric Dragonfly ...",
339
+ "source_name": "YouTube"
340
+ }
341
+ ]
342
+ ```
343
+ </response>
344
+
345
+ ## 3. Find Videos
346
+
347
+ <important>
348
+ First, you must first call the standard chat completion endpoint with your query.
349
+ The chat completion endpoint returns an ID, which must then be passed as the sole input parameter followup_id to the bagoodex/videos endpoint below.
350
+ </important>
351
+
352
+ ### Example:
353
+ <code_snippet>
354
+ ```py
355
+ import requests
356
+ from openai import OpenAI
357
+
358
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
359
+ API_KEY = '<YOUR_API_KEY>'
360
+ API_URL = 'https://api.aimlapi.com'
361
+
362
+ # Call the standart chat completion endpoint to get an ID
363
+ def complete_chat():
364
+ client = OpenAI(
365
+ base_url=API_URL,
366
+ api_key=API_KEY,
367
+ )
368
+
369
+ response = client.chat.completions.create(
370
+ model="bagoodex/bagoodex-search-v1",
371
+ messages=[
372
+ {
373
+ "role": "user",
374
+ "content": "how to work with github",
375
+ },
376
+ ],
377
+ )
378
+
379
+ # Extract the ID from the response
380
+ gen_id = response.id
381
+ print(f"Generated ID: {gen_id}")
382
+
383
+ # Call the Bagoodex endpoint with the generated ID
384
+ get_videos(gen_id)
385
+
386
+ def get_videos(gen_id):
387
+ params = {'followup_id': gen_id}
388
+ headers = {'Authorization': f'Bearer {API_KEY}'}
389
+ response = requests.get(f'{API_URL}/v1/bagoodex/videos', headers=headers, params=params)
390
+
391
+ print(response.json())
392
+
393
+ # Run the function
394
+ complete_chat()
395
+ ```
396
+
397
+ ### Model Response:
398
+
399
+ <response>
400
+ ```
401
+ [
402
+ {
403
+ "link": "https://www.youtube.com/watch?v=iv8rSLsi1xo",
404
+ "thumbnail": "https://dmwtgq8yidg0m.cloudfront.net/medium/_cYAcql_-g0w-video-thumb.jpeg",
405
+ "title": "GitHub Tutorial - Beginner's Training Guide"
406
+ },
407
+ {
408
+ "link": "https://www.youtube.com/watch?v=tRZGeaHPoaw",
409
+ "thumbnail": "https://dmwtgq8yidg0m.cloudfront.net/medium/-bforsTVDxRQ-video-thumb.jpeg",
410
+ "title": "Git and GitHub Tutorial for Beginners"
411
+ }
412
+ ]
413
+ ```
414
+ </response>
415
+
416
+ ## 4. Find a Local Map
417
+
418
+ <important>
419
+ First, you must first call the standard chat completion endpoint with your query.
420
+ The chat completion endpoint returns an ID, which must then be passed as the sole input parameter followup_id to the bagoodex/local-map endpoint below:
421
+ </important>
422
+
423
+ ### Example:
424
+
425
+ <code_snippet>
426
+ ```py
427
+ import requests
428
+ from openai import OpenAI
429
+
430
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
431
+ API_KEY = '<YOUR_API_KEY>'
432
+ API_URL = 'https://api.aimlapi.com'
433
+
434
+ # Call the standart chat completion endpoint to get an ID
435
+ def complete_chat():
436
+ client = OpenAI(
437
+ base_url=API_URL,
438
+ api_key=API_KEY,
439
+ )
440
+
441
+ response = client.chat.completions.create(
442
+ model="bagoodex/bagoodex-search-v1",
443
+ messages=[
444
+ {
445
+ "role": "user",
446
+ "content": "where is san francisco",
447
+ },
448
+ ],
449
+ )
450
+
451
+ # Extract the ID from the response
452
+ gen_id = response.id
453
+ print(f"Generated ID: {gen_id}")
454
+
455
+ # Call the Bagoodex endpoint with the generated ID
456
+ get_local_map(gen_id)
457
+
458
+ def get_local_map(gen_id):
459
+ params = {'followup_id': gen_id}
460
+ headers = {'Authorization': f'Bearer {API_KEY}'}
461
+ response = requests.get(f'{API_URL}/v1/bagoodex/local-map', headers=headers, params=params)
462
+
463
+ print(response.json())
464
+
465
+ # Run the function
466
+ complete_chat()
467
+ ```
468
+ </code_snippet>
469
+
470
+ ### Model Response:
471
+
472
+ <response>
473
+ ```
474
+ {
475
+ "link": "https://www.google.com/maps/place/San+Francisco,+CA/data=!4m2!3m1!1s0x80859a6d00690021:0x4a501367f076adff?sa=X&ved=2ahUKEwjqg7eNz9KLAxVCFFkFHWSPEeIQ8gF6BAgqEAA&hl=en",
476
+ "image": "https://dmwtgq8yidg0m.cloudfront.net/images/TdNFUpcEvvHL-local-map.webp"
477
+ }
478
+ ```
479
+ </response>
480
+
481
+ ## 5. Knowledge about a topic, structured as a small knowledge base
482
+
483
+ <important>
484
+ First, you must first call the standard chat completion endpoint with your query.
485
+ The chat completion endpoint returns an ID, which must then be passed as the sole input parameter followup_id to the bagoodex/knowledge endpoint below.
486
+ </important>
487
+
488
+ ### Example:
489
+
490
+ <code_snippet>
491
+ ```py
492
+ import requests
493
+ from openai import OpenAI
494
+
495
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
496
+ API_KEY = '<YOUR_API_KEY>'
497
+ API_URL = 'https://api.aimlapi.com'
498
+
499
+ # Call the standart chat completion endpoint to get an ID
500
+ def complete_chat():
501
+ client = OpenAI(
502
+ base_url=API_URL,
503
+ api_key=API_KEY,
504
+ )
505
+
506
+ response = client.chat.completions.create(
507
+ model="bagoodex/bagoodex-search-v1",
508
+ messages=[
509
+ {
510
+ "role": "user",
511
+ "content": "Who is Nicola Tesla",
512
+ },
513
+ ],
514
+ )
515
+
516
+ # Extract the ID from the response
517
+ gen_id = response.id
518
+ print(f"Generated ID: {gen_id}")
519
+
520
+ # Call the Bagoodex endpoint with the generated ID
521
+ get_knowledge(gen_id)
522
+
523
+ def get_knowledge(gen_id):
524
+ params = {'followup_id': gen_id}
525
+ headers = {'Authorization': f'Bearer {API_KEY}'}
526
+
527
+ response = requests.get(f'{API_URL}/v1/bagoodex/knowledge', headers=headers, params=params)
528
+ print(response.json())
529
+
530
+ # Run the function
531
+ complete_chat()
532
+ ```
533
+ </code_snippet>
534
+
535
+ ### Model Response:
536
+
537
+ <response>
538
+ ```
539
+ {
540
+ 'title': 'Nikola Tesla',
541
+ 'type': 'Engineer and futurist',
542
+ 'description': None,
543
+ 'born': 'July 10, 1856, Smiljan, Croatia',
544
+ 'died': 'January 7, 1943 (age 86 years), The New Yorker A Wyndham Hotel, New York, NY'
545
+ }
546
+ ```
547
+ </response>
548
+
549
+
550
+ [follow_up]:
551
+
552
+ Great!
553
+ Of course. This initial UI layout meet my expectations for the first step.
554
+ Please proceed with other steps as mentioned in the step-by-step guide above.
555
+
556
+ [follow_up]:
557
+
558
+ Great! But we need few changes.
559
+ 0. You forgot submit button. Stretch the UI (chat interface) full height. [always refer to "https://www.gradio.app/guides/creating-a-chatbot-fast" and follow up tutorials].
560
+ 1. Place Local Map Search and Knowledge Base above input in as a little buttons. They serve as an additional functionality for user query. If user selects one or both of them we should send additional API calls (maybe asynchronous) and return the results. Note that Local Map Search returns Google map url. We should render it instantly in place in the Gradio app. It would be great If we could render inside chat message big field.
561
+ 2. Seems you forgot Helper Functions and Classes for responses as we know they are static already. To display all the images as a Gallery on click Search Images. and on click we should expand image. ( refer to earlier message for more information and requirements and guidance ).
562
+ 3. Same applies to Search Videos, we should render them and on Click play instantly in place (NO redirect to YouTube). [( refer to earlier message for more information and requirements and guidance )].
563
+ 4. and Search Links we should render them accordingly: title then citation. For example: how_to_build_a_sling_at_home_thats_not_shit [here place link to redirect the user]. [( refer to earlier message for more information and requirements and guidance )].
564
+
565
+ [Search Images]:
566
+ <response>
567
+ [{'source': '', 'original': 'https://i.ytimg.com/vi/iYlJirFtYaA/sddefault.jpg', 'title': 'How to make a Slingshot using Pencils ...', 'source_name': 'YouTube'}, {'source': '', 'original': 'https://i.ytimg.com/vi/HWSkVaptzRA/maxresdefault.jpg', 'title': 'How to make a Slingshot at Home - YouTube', 'source_name': 'YouTube'}, {'source': '', 'original': 'https://content.instructables.com/FHB/VGF8/FHXUOJKJ/FHBVGF8FHXUOJKJ.jpg?auto=webp', 'title': 'Country Boy" Style Slingshot ...', 'source_name': 'Instructables'}, {'source': '', 'original': 'https://i.ytimg.com/vi/6wXqlJVw03U/maxresdefault.jpg', 'title': 'Make slingshot using popsicle stick ...', 'source_name': 'YouTube'}, {'source': '', 'original': 'https://ds-tc.prod.pbskids.org/designsquad/diy/DESIGN-SQUAD-42.jpg', 'title': 'Build | Indoor Slingshot . DESIGN SQUAD ...', 'source_name': 'PBS KIDS'}, {'source': '', 'original': 'https://i.ytimg.com/vi/wCxFkPLuNyA/maxresdefault.jpg', 'title': 'Paper Ninja Weapons ...', 'source_name': 'YouTube'}, {'source': '', 'original': 'https://i0.wp.com/makezine.com/wp-content/uploads/2015/01/slingshot1.jpg?fit=800%2C600&ssl=1', 'title': 'Rotating Bearings ...', 'source_name': 'Make Magazine'}, {'source': '', 'original': 'https://makeandtakes.com/wp-content/uploads/IMG_1144-1.jpg', 'title': 'Make a DIY Stick Slingshot Kids Craft', 'source_name': 'Make and Takes'}, {'source': '', 'original': 'https://i.ytimg.com/vi/X9oWGuKypuY/maxresdefault.jpg', 'title': 'Easy Home Made Slingshot - YouTube', 'source_name': 'YouTube'}, {'source': '', 'original': 'https://www.wikihow.com/images/thumb/4/41/Make-a-Sling-Shot-Step-7-Version-5.jpg/550px-nowatermark-Make-a-Sling-Shot-Step-7-Version-5.jpg', 'title': 'How to Make a Sling Shot: 15 Steps ...', 'source_name': 'wikiHow'}]
568
+ </response>
569
+
570
+ [Search Videos]:
571
+ <response>
572
+ Videos:
573
+ [{'link': 'https://www.youtube.com/watch?v=X9oWGuKypuY', 'thumbnail': 'https://dmwtgq8yidg0m.cloudfront.net/medium/d3G6HeC5BO93-video-thumb.jpeg', 'title': 'Easy Home Made Slingshot'}, {'link': 'https://www.youtube.com/watch?v=V2iZF8oAXHo&pp=ygUMI2d1bGVsaGFuZGxl', 'thumbnail': 'https://dmwtgq8yidg0m.cloudfront.net/medium/sb2Iw9Ug-Pne-video-thumb.jpeg', 'title': 'Making an Apple Wood Slingshot | Woodcraft'}]
574
+ </response>
575
+
576
+ [Links]:
577
+ <response>
578
+ ['https://www.reddit.com/r/slingshots/comments/1d50p3e/how_to_build_a_sling_at_home_thats_not_shit/', 'https://www.instructables.com/Make-a-Giant-Slingshot/', 'https://www.mudandbloom.com/blog/stick-slingshot', 'https://pbskids.org/designsquad/build/indoor-slingshot/', 'https://www.instructables.com/How-to-Make-a-Slingshot-2/']
579
+ </response>
580
+
581
+ ### Local Map Response:
582
+
583
+ <response>
584
+ {
585
+ "link": "https://www.google.com/maps/place/San+Francisco,+CA/data=!4m2!3m1!1s0x80859a6d00690021:0x4a501367f076adff?sa=X&ved=2ahUKEwjqg7eNz9KLAxVCFFkFHWSPEeIQ8gF6BAgqEAA&hl=en",
586
+ "image": "https://dmwtgq8yidg0m.cloudfront.net/images/TdNFUpcEvvHL-local-map.webp"
587
+ }
588
+
589
+ </response>
590
+
591
+
592
+ [follow_up]:
593
+
594
+ Great! Everything working really good!!
595
+ 1. Now let's reprodice using Gradios own specific components for Chat bots and AI applications.
596
+ 2. We can also simple replace all the helper function that used html and css to Gradio components.
597
+
598
+ Here's guide i just scraped from their website:
599
+ <start_of_guide>
600
+ How to Create a Chatbot with Gradio
601
+ Introduction
602
+ Chatbots are a popular application of large language models (LLMs). Using Gradio, you can easily build a chat application and share that with your users, or try it yourself using an intuitive UI.
603
+
604
+ This tutorial uses gr.ChatInterface(), which is a high-level abstraction that allows you to create your chatbot UI fast, often with a few lines of Python. It can be easily adapted to support multimodal chatbots, or chatbots that require further customization.
605
+
606
+ Prerequisites: please make sure you are using the latest version of Gradio:
607
+
608
+ $ pip install --upgrade gradio
609
+ Note for OpenAI-API compatible endpoints
610
+ If you have a chat server serving an OpenAI-API compatible endpoint (such as Ollama), you can spin up a ChatInterface in a single line of Python. First, also run pip install openai. Then, with your own URL, model, and optional token:
611
+
612
+ import gradio as gr
613
+
614
+ gr.load_chat("http://localhost:11434/v1/", model="llama3.2", token="***").launch()
615
+ Read about gr.load_chat in the docs. If you have your own model, keep reading to see how to create an application around any chat model in Python!
616
+
617
+ Defining a chat function
618
+ To create a chat application with gr.ChatInterface(), the first thing you should do is define your chat function. In the simplest case, your chat function should accept two arguments: message and history (the arguments can be named anything, but must be in this order).
619
+
620
+ message: a str representing the user's most recent message.
621
+ history: a list of openai-style dictionaries with role and content keys, representing the previous conversation history. May also include additional keys representing message metadata.
622
+ For example, the history could look like this:
623
+
624
+ [
625
+ {"role": "user", "content": "What is the capital of France?"},
626
+ {"role": "assistant", "content": "Paris"}
627
+ ]
628
+ while the next message would be:
629
+
630
+ "And what is its largest city?"
631
+ Your chat function simply needs to return:
632
+
633
+ a str value, which is the chatbot's response based on the chat history and most recent message, for example, in this case:
634
+ Paris is also the largest city.
635
+ Let's take a look at a few example chat functions:
636
+
637
+ Example: a chatbot that randomly responds with yes or no
638
+
639
+ Let's write a chat function that responds Yes or No randomly.
640
+
641
+ Here's our chat function:
642
+
643
+ import random
644
+
645
+ def random_response(message, history):
646
+ return random.choice(["Yes", "No"])
647
+ Now, we can plug this into gr.ChatInterface() and call the .launch() method to create the web interface:
648
+
649
+ import gradio as gr
650
+
651
+ gr.ChatInterface(
652
+ fn=random_response,
653
+ type="messages"
654
+ ).launch()
655
+ Tip:
656
+ Always set type="messages" in gr.ChatInterface. The default value (type="tuples") is deprecated and will be removed in a future version of Gradio.
657
+
658
+ That's it! Here's our running demo, try it out:
659
+
660
+ Chatbot
661
+ Message
662
+ Type a message...
663
+
664
+ gradio/chatinterface_random_response
665
+ built with Gradio.
666
+ Hosted on Hugging Face Space Spaces
667
+
668
+ Example: a chatbot that alternates between agreeing and disagreeing
669
+
670
+ Of course, the previous example was very simplistic, it didn't take user input or the previous history into account! Here's another simple example showing how to incorporate a user's input as well as the history.
671
+
672
+ import gradio as gr
673
+
674
+ def alternatingly_agree(message, history):
675
+ if len([h for h in history if h['role'] == "assistant"]) % 2 == 0:
676
+ return f"Yes, I do think that: {message}"
677
+ else:
678
+ return "I don't think so"
679
+
680
+ gr.ChatInterface(
681
+ fn=alternatingly_agree,
682
+ type="messages"
683
+ ).launch()
684
+ We'll look at more realistic examples of chat functions in our next Guide, which shows examples of using gr.ChatInterface with popular LLMs.
685
+
686
+ Streaming chatbots
687
+ In your chat function, you can use yield to generate a sequence of partial responses, each replacing the previous ones. This way, you'll end up with a streaming chatbot. It's that simple!
688
+
689
+ import time
690
+ import gradio as gr
691
+
692
+ def slow_echo(message, history):
693
+ for i in range(len(message)):
694
+ time.sleep(0.3)
695
+ yield "You typed: " + message[: i+1]
696
+
697
+ gr.ChatInterface(
698
+ fn=slow_echo,
699
+ type="messages"
700
+ ).launch()
701
+ While the response is streaming, the "Submit" button turns into a "Stop" button that can be used to stop the generator function.
702
+
703
+ Tip:
704
+ Even though you are yielding the latest message at each iteration, Gradio only sends the "diff" of each message from the server to the frontend, which reduces latency and data consumption over your network.
705
+
706
+ Customizing the Chat UI
707
+ If you're familiar with Gradio's gr.Interface class, the gr.ChatInterface includes many of the same arguments that you can use to customize the look and feel of your Chatbot. For example, you can:
708
+
709
+ add a title and description above your chatbot using title and description arguments.
710
+ add a theme or custom css using theme and css arguments respectively.
711
+ add examples and even enable cache_examples, which make your Chatbot easier for users to try it out.
712
+ customize the chatbot (e.g. to change the height or add a placeholder) or textbox (e.g. to add a max number of characters or add a placeholder).
713
+ Adding examples
714
+
715
+ You can add preset examples to your gr.ChatInterface with the examples parameter, which takes a list of string examples. Any examples will appear as "buttons" within the Chatbot before any messages are sent. If you'd like to include images or other files as part of your examples, you can do so by using this dictionary format for each example instead of a string: {"text": "What's in this image?", "files": ["cheetah.jpg"]}. Each file will be a separate message that is added to your Chatbot history.
716
+
717
+ You can change the displayed text for each example by using the example_labels argument. You can add icons to each example as well using the example_icons argument. Both of these arguments take a list of strings, which should be the same length as the examples list.
718
+
719
+ If you'd like to cache the examples so that they are pre-computed and the results appear instantly, set cache_examples=True.
720
+
721
+ Customizing the chatbot or textbox component
722
+
723
+ If you want to customize the gr.Chatbot or gr.Textbox that compose the ChatInterface, then you can pass in your own chatbot or textbox components. Here's an example of how we to apply the parameters we've discussed in this section:
724
+
725
+ import gradio as gr
726
+
727
+ def yes_man(message, history):
728
+ if message.endswith("?"):
729
+ return "Yes"
730
+ else:
731
+ return "Ask me anything!"
732
+
733
+ gr.ChatInterface(
734
+ yes_man,
735
+ type="messages",
736
+ chatbot=gr.Chatbot(height=300),
737
+ textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
738
+ title="Yes Man",
739
+ description="Ask Yes Man any question",
740
+ theme="ocean",
741
+ examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
742
+ cache_examples=True,
743
+ ).launch()
744
+ Here's another example that adds a "placeholder" for your chat interface, which appears before the user has started chatting. The placeholder argument of gr.Chatbot accepts Markdown or HTML:
745
+
746
+ gr.ChatInterface(
747
+ yes_man,
748
+ type="messages",
749
+ chatbot=gr.Chatbot(placeholder="<strong>Your Personal Yes-Man</strong><br>Ask Me Anything"),
750
+ ...
751
+ The placeholder appears vertically and horizontally centered in the chatbot.
752
+
753
+ Multimodal Chat Interface
754
+ You may want to add multimodal capabilities to your chat interface. For example, you may want users to be able to upload images or files to your chatbot and ask questions about them. You can make your chatbot "multimodal" by passing in a single parameter (multimodal=True) to the gr.ChatInterface class.
755
+
756
+ When multimodal=True, the signature of your chat function changes slightly: the first parameter of your function (what we referred to as message above) should accept a dictionary consisting of the submitted text and uploaded files that looks like this:
757
+
758
+ {
759
+ "text": "user input",
760
+ "files": [
761
+ "updated_file_1_path.ext",
762
+ "updated_file_2_path.ext",
763
+ ...
764
+ ]
765
+ }
766
+ This second parameter of your chat function, history, will be in the same openai-style dictionary format as before. However, if the history contains uploaded files, the content key for a file will be not a string, but rather a single-element tuple consisting of the filepath. Each file will be a separate message in the history. So after uploading two files and asking a question, your history might look like this:
767
+
768
+ [
769
+ {"role": "user", "content": ("cat1.png")},
770
+ {"role": "user", "content": ("cat2.png")},
771
+ {"role": "user", "content": "What's the difference between these two images?"},
772
+ ]
773
+ The return type of your chat function does not change when setting multimodal=True (i.e. in the simplest case, you should still return a string value). We discuss more complex cases, e.g. returning files below.
774
+
775
+ If you are customizing a multimodal chat interface, you should pass in an instance of gr.MultimodalTextbox to the textbox parameter. You can customize the MultimodalTextbox further by passing in the sources parameter, which is a list of sources to enable. Here's an example that illustrates how to set up and customize and multimodal chat interface:
776
+
777
+ import gradio as gr
778
+
779
+ def count_images(message, history):
780
+ num_images = len(message["files"])
781
+ total_images = 0
782
+ for message in history:
783
+ if isinstance(message["content"], tuple):
784
+ total_images += 1
785
+ return f"You just uploaded {num_images} images, total uploaded: {total_images+num_images}"
786
+
787
+ demo = gr.ChatInterface(
788
+ fn=count_images,
789
+ type="messages",
790
+ examples=[
791
+ {"text": "No files", "files": []}
792
+ ],
793
+ multimodal=True,
794
+ textbox=gr.MultimodalTextbox(file_count="multiple", file_types=["image"], sources=["upload", "microphone"])
795
+ )
796
+
797
+ demo.launch()
798
+ Additional Inputs
799
+ You may want to add additional inputs to your chat function and expose them to your users through the chat UI. For example, you could add a textbox for a system prompt, or a slider that sets the number of tokens in the chatbot's response. The gr.ChatInterface class supports an additional_inputs parameter which can be used to add additional input components.
800
+
801
+ The additional_inputs parameters accepts a component or a list of components. You can pass the component instances directly, or use their string shortcuts (e.g. "textbox" instead of gr.Textbox()). If you pass in component instances, and they have not already been rendered, then the components will appear underneath the chatbot within a gr.Accordion().
802
+
803
+ Here's a complete example:
804
+
805
+ import gradio as gr
806
+ import time
807
+
808
+ def echo(message, history, system_prompt, tokens):
809
+ response = f"System prompt: {system_prompt}\n Message: {message}."
810
+ for i in range(min(len(response), int(tokens))):
811
+ time.sleep(0.05)
812
+ yield response[: i + 1]
813
+
814
+ demo = gr.ChatInterface(
815
+ echo,
816
+ type="messages",
817
+ additional_inputs=[
818
+ gr.Textbox("You are helpful AI.", label="System Prompt"),
819
+ gr.Slider(10, 100),
820
+ ],
821
+ )
822
+
823
+ demo.launch()
824
+ If the components you pass into the additional_inputs have already been rendered in a parent gr.Blocks(), then they will not be re-rendered in the accordion. This provides flexibility in deciding where to lay out the input components. In the example below, we position the gr.Textbox() on top of the Chatbot UI, while keeping the slider underneath.
825
+
826
+ import gradio as gr
827
+ import time
828
+
829
+ def echo(message, history, system_prompt, tokens):
830
+ response = f"System prompt: {system_prompt}\n Message: {message}."
831
+ for i in range(min(len(response), int(tokens))):
832
+ time.sleep(0.05)
833
+ yield response[: i+1]
834
+
835
+ with gr.Blocks() as demo:
836
+ system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
837
+ slider = gr.Slider(10, 100, render=False)
838
+
839
+ gr.ChatInterface(
840
+ echo, additional_inputs=[system_prompt, slider], type="messages"
841
+ )
842
+
843
+ demo.launch()
844
+ Examples with additional inputs
845
+
846
+ You can also add example values for your additional inputs. Pass in a list of lists to the examples parameter, where each inner list represents one sample, and each inner list should be 1 + len(additional_inputs) long. The first element in the inner list should be the example value for the chat message, and each subsequent element should be an example value for one of the additional inputs, in order. When additional inputs are provided, examples are rendered in a table underneath the chat interface.
847
+
848
+ If you need to create something even more custom, then its best to construct the chatbot UI using the low-level gr.Blocks() API. We have a dedicated guide for that here.
849
+
850
+ Additional Outputs
851
+ In the same way that you can accept additional inputs into your chat function, you can also return additional outputs. Simply pass in a list of components to the additional_outputs parameter in gr.ChatInterface and return additional values for each component from your chat function. Here's an example that extracts code and outputs it into a separate gr.Code component:
852
+
853
+ import gradio as gr
854
+
855
+ python_code = """
856
+ def fib(n):
857
+ if n <= 0:
858
+ return 0
859
+ elif n == 1:
860
+ return 1
861
+ else:
862
+ return fib(n-1) + fib(n-2)
863
+ """
864
+
865
+ js_code = """
866
+ function fib(n) {
867
+ if (n <= 0) return 0;
868
+ if (n === 1) return 1;
869
+ return fib(n - 1) + fib(n - 2);
870
+ }
871
+ """
872
+
873
+ def chat(message, history):
874
+ if "python" in message.lower():
875
+ return "Type Python or JavaScript to see the code.", gr.Code(language="python", value=python_code)
876
+ elif "javascript" in message.lower():
877
+ return "Type Python or JavaScript to see the code.", gr.Code(language="javascript", value=js_code)
878
+ else:
879
+ return "Please ask about Python or JavaScript.", None
880
+
881
+ with gr.Blocks() as demo:
882
+ code = gr.Code(render=False)
883
+ with gr.Row():
884
+ with gr.Column():
885
+ gr.Markdown("<center><h1>Write Python or JavaScript</h1></center>")
886
+ gr.ChatInterface(
887
+ chat,
888
+ examples=["Python", "JavaScript"],
889
+ additional_outputs=[code],
890
+ type="messages"
891
+ )
892
+ with gr.Column():
893
+ gr.Markdown("<center><h1>Code Artifacts</h1></center>")
894
+ code.render()
895
+
896
+ demo.launch()
897
+ Note: unlike the case of additional inputs, the components passed in additional_outputs must be already defined in your gr.Blocks context -- they are not rendered automatically. If you need to render them after your gr.ChatInterface, you can set render=False when they are first defined and then .render() them in the appropriate section of your gr.Blocks() as we do in the example above.
898
+
899
+ Returning Complex Responses
900
+ We mentioned earlier that in the simplest case, your chat function should return a str response, which will be rendered as Markdown in the chatbot. However, you can also return more complex responses as we discuss below:
901
+
902
+ Returning files or Gradio components
903
+
904
+ Currently, the following Gradio components can be displayed inside the chat interface:
905
+
906
+ gr.Image
907
+ gr.Plot
908
+ gr.Audio
909
+ gr.HTML
910
+ gr.Video
911
+ gr.Gallery
912
+ gr.File
913
+ Simply return one of these components from your function to use it with gr.ChatInterface. Here's an example that returns an audio file:
914
+
915
+ import gradio as gr
916
+
917
+ def music(message, history):
918
+ if message.strip():
919
+ return gr.Audio("https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav")
920
+ else:
921
+ return "Please provide the name of an artist"
922
+
923
+ gr.ChatInterface(
924
+ music,
925
+ type="messages",
926
+ textbox=gr.Textbox(placeholder="Which artist's music do you want to listen to?", scale=7),
927
+ ).launch()
928
+ Similarly, you could return image files with gr.Image, video files with gr.Video, or arbitrary files with the gr.File component.
929
+
930
+ Returning Multiple Messages
931
+
932
+ You can return multiple assistant messages from your chat function simply by returning a list of messages, each of which is a valid chat type. This lets you, for example, send a message along with files, as in the following example:
933
+
934
+ import gradio as gr
935
+
936
+ def echo_multimodal(message, history):
937
+ response = []
938
+ response.append("You wrote: '" + message["text"] + "' and uploaded:")
939
+ if message.get("files"):
940
+ for file in message["files"]:
941
+ response.append(gr.File(value=file))
942
+ return response
943
+
944
+ demo = gr.ChatInterface(
945
+ echo_multimodal,
946
+ type="messages",
947
+ multimodal=True,
948
+ textbox=gr.MultimodalTextbox(file_count="multiple"),
949
+ )
950
+
951
+ demo.launch()
952
+ Displaying intermediate thoughts or tool usage
953
+
954
+ The gr.ChatInterface class supports displaying intermediate thoughts or tool usage direct in the chatbot.
955
+
956
+
957
+
958
+ To do this, you will need to return a gr.ChatMessage object from your chat function. Here is the schema of the gr.ChatMessage data class as well as two internal typed dictionaries:
959
+
960
+ @dataclass
961
+ class ChatMessage:
962
+ content: str | Component
963
+ metadata: MetadataDict = None
964
+ options: list[OptionDict] = None
965
+
966
+ class MetadataDict(TypedDict):
967
+ title: NotRequired[str]
968
+ id: NotRequired[int | str]
969
+ parent_id: NotRequired[int | str]
970
+ log: NotRequired[str]
971
+ duration: NotRequired[float]
972
+ status: NotRequired[Literal["pending", "done"]]
973
+
974
+ class OptionDict(TypedDict):
975
+ label: NotRequired[str]
976
+ value: str
977
+ As you can see, the gr.ChatMessage dataclass is similar to the openai-style message format, e.g. it has a "content" key that refers to the chat message content. But it also includes a "metadata" key whose value is a dictionary. If this dictionary includes a "title" key, the resulting message is displayed as an intermediate thought with the title being displayed on top of the thought. Here's an example showing the usage:
978
+
979
+ import gradio as gr
980
+ from gradio import ChatMessage
981
+ import time
982
+
983
+ sleep_time = 0.5
984
+
985
+ def simulate_thinking_chat(message, history):
986
+ start_time = time.time()
987
+ response = ChatMessage(
988
+ content="",
989
+ metadata={"title": "_Thinking_ step-by-step", "id": 0, "status": "pending"}
990
+ )
991
+ yield response
992
+
993
+ thoughts = [
994
+ "First, I need to understand the core aspects of the query...",
995
+ "Now, considering the broader context and implications...",
996
+ "Analyzing potential approaches to formulate a comprehensive answer...",
997
+ "Finally, structuring the response for clarity and completeness..."
998
+ ]
999
+
1000
+ accumulated_thoughts = ""
1001
+ for thought in thoughts:
1002
+ time.sleep(sleep_time)
1003
+ accumulated_thoughts += f"- {thought}\n\n"
1004
+ response.content = accumulated_thoughts.strip()
1005
+ yield response
1006
+
1007
+ response.metadata["status"] = "done"
1008
+ response.metadata["duration"] = time.time() - start_time
1009
+ yield response
1010
+
1011
+ response = [
1012
+ response,
1013
+ ChatMessage(
1014
+ content="Based on my thoughts and analysis above, my response is: This dummy repro shows how thoughts of a thinking LLM can be progressively shown before providing its final answer."
1015
+ )
1016
+ ]
1017
+ yield response
1018
+
1019
+
1020
+ demo = gr.ChatInterface(
1021
+ simulate_thinking_chat,
1022
+ title="Thinking LLM Chat Interface 🤔",
1023
+ type="messages",
1024
+ )
1025
+
1026
+ demo.launch()
1027
+ You can even show nested thoughts, which is useful for agent demos in which one tool may call other tools. To display nested thoughts, include "id" and "parent_id" keys in the "metadata" dictionary. Read our dedicated guide on displaying intermediate thoughts and tool usage for more realistic examples.
1028
+
1029
+ Providing preset responses
1030
+
1031
+ When returning an assistant message, you may want to provide preset options that a user can choose in response. To do this, again, you will again return a gr.ChatMessage instance from your chat function. This time, make sure to set the options key specifying the preset responses.
1032
+
1033
+ As shown in the schema for gr.ChatMessage above, the value corresponding to the options key should be a list of dictionaries, each with a value (a string that is the value that should be sent to the chat function when this response is clicked) and an optional label (if provided, is the text displayed as the preset response instead of the value).
1034
+
1035
+ This example illustrates how to use preset responses:
1036
+
1037
+ import gradio as gr
1038
+ import random
1039
+
1040
+ example_code = """
1041
+ Here's an example Python lambda function:
1042
+
1043
+ lambda x: x + {}
1044
+
1045
+ Is this correct?
1046
+ """
1047
+
1048
+ def chat(message, history):
1049
+ if message == "Yes, that's correct.":
1050
+ return "Great!"
1051
+ else:
1052
+ return gr.ChatMessage(
1053
+ content=example_code.format(random.randint(1, 100)),
1054
+ options=[
1055
+ {"value": "Yes, that's correct.", "label": "Yes"},
1056
+ {"value": "No"}
1057
+ ]
1058
+ )
1059
+
1060
+ demo = gr.ChatInterface(
1061
+ chat,
1062
+ type="messages",
1063
+ examples=["Write an example Python lambda function."]
1064
+ )
1065
+
1066
+ demo.launch()
1067
+ Modifying the Chatbot Value Directly
1068
+ You may wish to modify the value of the chatbot with your own events, other than those prebuilt in the gr.ChatInterface. For example, you could create a dropdown that prefills the chat history with certain conversations or add a separate button to clear the conversation history. The gr.ChatInterface supports these events, but you need to use the gr.ChatInterface.chatbot_value as the input or output component in such events. In this example, we use a gr.Radio component to prefill the the chatbot with certain conversations:
1069
+
1070
+ import gradio as gr
1071
+ import random
1072
+
1073
+ def prefill_chatbot(choice):
1074
+ if choice == "Greeting":
1075
+ return [
1076
+ {"role": "user", "content": "Hi there!"},
1077
+ {"role": "assistant", "content": "Hello! How can I assist you today?"}
1078
+ ]
1079
+ elif choice == "Complaint":
1080
+ return [
1081
+ {"role": "user", "content": "I'm not happy with the service."},
1082
+ {"role": "assistant", "content": "I'm sorry to hear that. Can you please tell me more about the issue?"}
1083
+ ]
1084
+ else:
1085
+ return []
1086
+
1087
+ def random_response(message, history):
1088
+ return random.choice(["Yes", "No"])
1089
+
1090
+ with gr.Blocks() as demo:
1091
+ radio = gr.Radio(["Greeting", "Complaint", "Blank"])
1092
+ chat = gr.ChatInterface(random_response, type="messages")
1093
+ radio.change(prefill_chatbot, radio, chat.chatbot_value)
1094
+
1095
+ demo.launch()
1096
+ Using Your Chatbot via API
1097
+ Once you've built your Gradio chat interface and are hosting it on Hugging Face Spaces or somewhere else, then you can query it with a simple API at the /chat endpoint. The endpoint just expects the user's message and will return the response, internally keeping track of the message history.
1098
+
1099
+
1100
+
1101
+ To use the endpoint, you should use either the Gradio Python Client or the Gradio JS client. Or, you can deploy your Chat Interface to other platforms, such as a:
1102
+
1103
+ Discord bot [tutorial]
1104
+ Slack bot [tutorial]
1105
+ Website widget [tutorial]
1106
+ Chat History
1107
+ You can enable persistent chat history for your ChatInterface, allowing users to maintain multiple conversations and easily switch between them. When enabled, conversations are stored locally and privately in the user's browser using local storage. So if you deploy a ChatInterface e.g. on Hugging Face Spaces, each user will have their own separate chat history that won't interfere with other users' conversations. This means multiple users can interact with the same ChatInterface simultaneously while maintaining their own private conversation histories.
1108
+
1109
+ To enable this feature, simply set gr.ChatInterface(save_history=True) (as shown in the example in the next section). Users will then see their previous conversations in a side panel and can continue any previous chat or start a new one.
1110
+
1111
+ Collecting User Feedback
1112
+ To gather feedback on your chat model, set gr.ChatInterface(flagging_mode="manual") and users will be able to thumbs-up or thumbs-down assistant responses. Each flagged response, along with the entire chat history, will get saved in a CSV file in the app working directory (this can be configured via the flagging_dir parameter).
1113
+
1114
+ You can also change the feedback options via flagging_options parameter. The default options are "Like" and "Dislike", which appear as the thumbs-up and thumbs-down icons. Any other options appear under a dedicated flag icon. This example shows a ChatInterface that has both chat history (mentioned in the previous section) and user feedback enabled:
1115
+
1116
+ import time
1117
+ import gradio as gr
1118
+
1119
+ def slow_echo(message, history):
1120
+ for i in range(len(message)):
1121
+ time.sleep(0.05)
1122
+ yield "You typed: " + message[: i + 1]
1123
+
1124
+ demo = gr.ChatInterface(
1125
+ slow_echo,
1126
+ type="messages",
1127
+ flagging_mode="manual",
1128
+ flagging_options=["Like", "Spam", "Inappropriate", "Other"],
1129
+ save_history=True,
1130
+ )
1131
+
1132
+ demo.launch()
1133
+ Note that in this example, we set several flagging options: "Like", "Spam", "Inappropriate", "Other". Because the case-sensitive string "Like" is one of the flagging options, the user will see a thumbs-up icon next to each assistant message. The three other flagging options will appear in a dropdown under the flag icon.
1134
+
1135
+ What's Next?
1136
+ Now that you've learned about the gr.ChatInterface class and how it can be used to create chatbot UIs quickly, we recommend reading one of the following:
1137
+
1138
+ Our next Guide shows examples of how to use gr.ChatInterface with popular LLM libraries.
1139
+ If you'd like to build very custom chat applications from scratch, you can build them using the low-level Blocks API, as discussed in this Guide.
1140
+ Once you've deployed your Gradio Chat Interface, its easy to use it other applications because of the built-in API. Here's a tutorial on how to deploy a Gradio chat interface as a Discord bot.
1141
+ <end_of_guide>
1142
+
1143
+ [follow_up]:
1144
+
1145
+ No make sure to keep the same UI as before in two columns. Place Knowledge Base and Local Map above grade chatinterface input and make them checkbox. when they checked we will do additional async request to the corresponding API. and display one by one separated messages in side chat interface. on the right side leave the Search Images, Videos, and Links.
1146
+
1147
+ [follow_up]:
1148
+
1149
+ <example_return_response>
1150
+ If you're planning to visit Paris, there are several options to consider for a 2-week trip.
1151
+
1152
+ You can start by exploring the surrounding areas of Paris, such as Versailles and Giverny, Monet's Garden, which are easily accessible by public transportation.
1153
+
1154
+ Another option is to visit the Loire Valley, which is known for its beautiful chateaux, such as Chambord, Chenonceau, and Tours. However, renting a car from Paris might be challenging without a driver's license, and the cost of an automatic rental might be out of your budget.
1155
+
1156
+ Normandy is another region worth considering, with its D-day beaches, Bayeux tapestry, and Mont St Michel. However, this region is also best explored by car, and past reviews of local tours have been disappointing.
1157
+
1158
+ Alsace is a beautiful region with the city of Strasbourg, which is highly recommended. However, it might be a bit out of the way, and a 1-2 day trip might not be enough to fully experience the region.
1159
+
1160
+ Provence is another option, with its charming cities like Montpellier, Marseille, Nice, and St Tropex. However, this region is also best explored by car, and it might be more enjoyable if you're on a honeymoon or have more time to stay around the sea.
1161
+
1162
+ To get a better idea of the best combination of cities and regions to visit with Paris, you can check out the following resources:
1163
+
1164
+ https://www.fodors.com/community/europe/pls-suggest-combination-of-paris-and-which-other-cities-regions-for-2-week-trip-592935/
1165
+ https://www.fodors.com/community/europe/pls-suggest-combination-of-paris-and-which-other-cities-regions-for-2-week-trip-592935/#post15592935
1166
+ https://www.fodors.com/community/europe/pls-suggest-combination-of-paris-and-which-other-cities-regions-for-2-week-trip-592935/#post15592935
1167
+
1168
+ </example_return_response>
1169
+
1170
+
1171
+ [follow_up]:
1172
+
1173
+
1174
+
1175
+
1176
+
1177
+ [follow_up]:
1178
+
1179
+ 1. Update and Refactor codebase of the following Gradio App.
1180
+ 2. Use helpers from `helpers.py`. (e.g., embed_video, embed_image, format_links, embed_google_map, format_knowledge, and )
1181
+ 3. Implement follow up questions to be displayed after each conversation below input field. on click any of them: question should be added to the ChatInterface conversation. 3.1. Send request to `chat_function`.
1182
+ 4. List follow up questions instantly after getting response from `def chat_function // client.complete_chat(message) //`.
1183
+ 5. if there is any issues onClick local map. Then we should send request to `get_places`.
1184
+ 6. We are not pushing you to append `local map` and `knowledge base` response to the ChatInterface. You are all free to display them separately as we are displaying the `Search Images` and `Search Videos` in different cards interfaces.
1185
+
1186
+ ####### Gradio App #######
1187
+ [app.py]:
1188
+ <code_snippet>
1189
+ import os
1190
+ import requests
1191
+ import gradio as gr
1192
+ from bagoodex_client import BagoodexClient
1193
+ from r_types import ChatMessage
1194
+ from prompts import SYSTEM_PROMPT_FOLLOWUP, SYSTEM_PROMPT_MAP, SYSTEM_PROMPT_BASE
1195
+ from helpers import format_followup_questions
1196
+
1197
+ client = BagoodexClient()
1198
+
1199
+ def format_knowledge(result):
1200
+ title = result.get('title', 'Unknown')
1201
+ type_ = result.get('type', '')
1202
+ born = result.get('born', '')
1203
+ died = result.get('died', '')
1204
+ content = f"""
1205
+ **{title}**
1206
+ Type: {type_}
1207
+ Born: {born}
1208
+ Died: {died}
1209
+ """
1210
+ return gr.Markdown(content)
1211
+
1212
+ def format_images(result):
1213
+ urls = [item.get("original", "") for item in result]
1214
+ return urls
1215
+
1216
+ # Helper formatting functions
1217
+ def format_videos(result):
1218
+ return [vid.get('link', '') for vid in result]
1219
+
1220
+ # Advanced search functions
1221
+ def perform_video_search(followup_id):
1222
+ if not followup_id:
1223
+ return []
1224
+ result = client.get_videos(followup_id)
1225
+ return format_videos(result)
1226
+
1227
+ def format_links(result):
1228
+ links_md = "**Links:**\n"
1229
+ for url in result:
1230
+ title = url.rstrip('/').split('/')[-1]
1231
+ links_md += f"- [{title}]({url})\n"
1232
+ return gr.Markdown(links_md)
1233
+
1234
+ # Define the chat function
1235
+ def chat_function(message, history, followup_id):
1236
+ followup_id_new, answer = client.complete_chat(message)
1237
+ return answer, followup_id_new
1238
+
1239
+ def format_local_map(result):
1240
+ link = result.get('link', '')
1241
+ image_url = result.get('image', '')
1242
+ html = f"""
1243
+ <div>
1244
+ <strong>Local Map:</strong><br>
1245
+ <a href='{link}' target='_blank'>View on Google Maps</a><br>
1246
+ <img src='{image_url}' style='width:100%;'/>
1247
+ </div>
1248
+ """
1249
+ return gr.HTML(html)
1250
+
1251
+ def append_local_map(followup_id, chatbot_value):
1252
+ if not followup_id:
1253
+ return chatbot_value
1254
+ result = client.get_local_map(followup_id)
1255
+ formatted = format_local_map(result)
1256
+ new_message = {"role": "assistant", "content": formatted}
1257
+ return chatbot_value + [new_message]
1258
+
1259
+ def append_knowledge(followup_id, chatbot_value):
1260
+ if not followup_id:
1261
+ return chatbot_value
1262
+ result = client.get_knowledge(followup_id)
1263
+ formatted = format_knowledge(result)
1264
+ new_message = {"role": "assistant", "content": formatted}
1265
+ return chatbot_value + [new_message]
1266
+
1267
+ # Define advanced search functions
1268
+ def perform_image_search(followup_id):
1269
+ if not followup_id:
1270
+ return []
1271
+ result = client.get_images(followup_id)
1272
+ urls = format_images(result)
1273
+ return urls
1274
+
1275
+ def perform_links_search(followup_id):
1276
+ if not followup_id:
1277
+ return gr.Markdown("No followup ID available.")
1278
+ result = client.get_links(followup_id)
1279
+ return format_links(result)
1280
+
1281
+ # Custom CSS
1282
+ css = """
1283
+ #chatbot {
1284
+ height: 100%;
1285
+ }
1286
+ """
1287
+
1288
+
1289
+ def list_followup_questions(followup_id):
1290
+ if not followup_id:
1291
+ return gr.Markdown("No followup ID available.")
1292
+ result = client.base_qna(messages=chat, system_prompt=SYSTEM_PROMPT_FOLLOWUP)
1293
+ return format_followup_questions(result)
1294
+
1295
+ def get_places(followup_id):
1296
+ if not followup_id:
1297
+ return gr.Markdown("No followup ID available.")
1298
+ result = client.base_qna(messages=chat, system_prompt=SYSTEM_PROMPT_MAP)
1299
+ return format_places(result)
1300
+
1301
+ # Build UI
1302
+ with gr.Blocks(css=css, fill_height=True) as demo:
1303
+ followup_state = gr.State(None)
1304
+ with gr.Row():
1305
+ with gr.Column(scale=3):
1306
+ with gr.Row():
1307
+ btn_local_map = gr.Button("Local Map Search", variant="secondary", size="sm")
1308
+ btn_knowledge = gr.Button("Knowledge Base", variant="secondary", size="sm")
1309
+ chat = gr.ChatInterface(
1310
+ fn=chat_function,
1311
+ type="messages",
1312
+ additional_inputs=[followup_state],
1313
+ additional_outputs=[followup_state],
1314
+ )
1315
+ # Wire up the buttons to append to chat history
1316
+ btn_local_map.click(
1317
+ append_local_map,
1318
+ inputs=[followup_state, chat.chatbot],
1319
+ outputs=chat.chatbot
1320
+ )
1321
+ btn_knowledge.click(
1322
+ append_knowledge,
1323
+ inputs=[followup_state, chat.chatbot],
1324
+ outputs=chat.chatbot
1325
+ )
1326
+ with gr.Column(scale=1):
1327
+ gr.Markdown("### Advanced Search Options")
1328
+ with gr.Column(variant="panel"):
1329
+ btn_images = gr.Button("Search Images")
1330
+ btn_videos = gr.Button("Search Videos")
1331
+ btn_links = gr.Button("Search Links")
1332
+ gallery_output = gr.Gallery(label="Image Results", columns=2)
1333
+ video_output = gr.Gallery(label="Video Results", columns=1, visible=True)
1334
+ links_output = gr.Markdown(label="Links Results")
1335
+ btn_images.click(
1336
+ perform_image_search,
1337
+ inputs=[followup_state],
1338
+ outputs=[gallery_output]
1339
+ )
1340
+ btn_videos.click(
1341
+ perform_video_search,
1342
+ inputs=[followup_state],
1343
+ outputs=[video_output]
1344
+ )
1345
+ btn_links.click(
1346
+ perform_links_search,
1347
+ inputs=[followup_state],
1348
+ outputs=[links_output]
1349
+ )
1350
+ demo.launch()
1351
+ </code_snippet>
1352
+
1353
+ [helper.py]:
1354
+ <code_snippet>
1355
+ # old code helpers as it was earlier.
1356
+ # embed_video,
1357
+ # embed_image,
1358
+ # format_links,
1359
+ # embed_google_map,
1360
+ # format_knowledge
1361
+
1362
+ # newly added. Note: fix it (as you did earlier with other helpers) if `format_followup_questions` has any issues.
1363
+
1364
+ def format_followup_questions(questions: List[str]) -> str:
1365
+ """
1366
+ Given a list of follow-up questions, return a Markdown string
1367
+ with each question as a bulleted list item.
1368
+ """
1369
+ if not questions:
1370
+ return "No follow-up questions provided."
1371
+
1372
+ questions_md = "### Follow-up Questions\n\n"
1373
+ for question in questions:
1374
+ questions_md += f"- {question}\n"
1375
+ return questions_md
1376
+ </code_snippet>
1377
+
1378
+
1379
+ [follow_up]:
1380
+
1381
+ Implement Parsing:
1382
+ Make sure to extract the data and parse it properly:
1383
+ ----------
1384
+ def format_followup_questions(questions) -> str:
1385
+ """
1386
+ questions are exactly same as this:
1387
+
1388
+ json
1389
+ {
1390
+ "followup_question": ["What materials are needed to make a slingshot?", "How to make a slingshot more powerful?"]
1391
+ }
1392
+
1393
+ """
1394
+ if not questions:
1395
+ return "No follow-up questions provided."
1396
+
1397
+ questions_md = "### Follow-up Questions\n\n"
1398
+ for question in questions:
1399
+ questions_md += f"- {question}\n"
1400
+ return questions_md
1401
+
1402
+
1403
+ [follow_up]:
1404
+
1405
+ i was lazy to put them
1406
+
1407
+ make sure to remove the " json " before parsing.
1408
+
1409
+ [follow_up]:
1410
+
1411
+ No need to display two times follow up questions. Remove second one. Radio buttons enough.
1412
+ ---------------------
1413
+
1414
+ # Below the chat input, display follow-up questions and let user select one.
1415
+ followup_radio = gr.Radio(
1416
+ choices=[], label="Follow-up Questions (select one and click Send Follow-up)"
1417
+ )
1418
+ btn_send_followup = gr.Button("Send Follow-up")
1419
+ # When a follow-up question is sent, update the chat conversation, followup state, and follow-up list.
1420
+ btn_send_followup.click(
1421
+ fn=handle_followup_click,
1422
+ inputs=[followup_radio, followup_state, chat_history_state],
1423
+ outputs=[chat.chatbot, followup_state, followup_md_state]
1424
+ )
1425
+ # Also display the follow-up questions markdown (for reference) in a Markdown component.
1426
+ followup_markdown = gr.Markdown(label="Follow-up Questions", value="", visible=True)
1427
+ # When the followup_md_state updates, also update the radio choices.
1428
+ def update_followup_radio(md_text):
1429
+ # Assume the helper output is a Markdown string with list items.
1430
+ # We split the text to extract the question lines.
1431
+ lines = md_text.splitlines()
1432
+ questions = []
1433
+ for line in lines:
1434
+ if line.startswith("- "):
1435
+ questions.append(line[2:])
1436
+ return gr.update(choices=questions, value=None), md_text
1437
+ followup_md_state.change(
1438
+ fn=update_followup_radio,
1439
+ inputs=[followup_md_state],
1440
+ outputs=[followup_radio, followup_markdown]
1441
+ )
1442
+
1443
+ [follow_up]:
1444
+
1445
+
1446
+
1447
+
1448
+ [follow_up]:
1449
+
1450
+
1451
+
1452
+
1453
+ [follow_up]:
prompts.py CHANGED
@@ -43,4 +43,24 @@ Here's JSON format example:
43
  ######NOTE######
44
  Make sure to return only JSON data! Nothing else!
45
  ######SYSTEM SHUTDOWN######
46
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  ######NOTE######
44
  Make sure to return only JSON data! Nothing else!
45
  ######SYSTEM SHUTDOWN######
46
+ """
47
+
48
+ SYSTEM_PROMPT_KNOWLEDGE_BASE = """
49
+ ######SYSTEM INIATED######
50
+ You will be given a content from conversation chat (e.g., text/ paragraph).
51
+ Your task is to analyze the given content and provide a knowledge base response based on the given content.
52
+ For example: If the given content (conversation chat) was about "How to make a slingshot".
53
+ You should analyze it and find the exact creator or founder or inventor of the slingshot.
54
+ Let's assume you just found out that the slingshot was invented by "Charles Goodyear".
55
+ Then return `question` in a JSON format. (e.g., {"question": "Who is Charles Goodyear?"}).
56
+ Your final output should be a JSON data with the knowledge base response.
57
+ Here's JSON format example:
58
+ ```json
59
+ {
60
+ "question": "Who is Charles Goodyear?",
61
+ }
62
+ ```
63
+ ######NOTE######
64
+ Make sure to return only JSON data! Nothing else!
65
+ ######SYSTEM SHUTDOWN######
66
+ """
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  openai
2
  gradio
3
  python-dotenv
4
- requests
 
 
1
  openai
2
  gradio
3
  python-dotenv
4
+ requests
5
+ pytube
test_api.py CHANGED
@@ -1,21 +1,35 @@
1
- import os
2
  from openai import OpenAI
 
 
 
 
 
 
 
3
 
4
- client = OpenAI(
5
- base_url="https://api.aimlapi.com/v1",
6
- api_key=os.getenv("AIML_API_KEY"),
7
- )
 
 
8
 
9
- response = client.chat.completions.create(
10
- model="gpt-4o",
11
- messages=[
12
- {
13
- "role": "user",
14
- "content": "Tell me, why is the sky blue?"
15
- },
16
- ],
17
- )
 
 
 
 
 
18
 
19
- message = response.choices[0].message.content
20
 
21
- print(f"Assistant: {message}")
 
 
1
+ import requests
2
  from openai import OpenAI
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+ # Insert your AIML API Key instead of <YOUR_API_KEY>:
8
+ API_KEY = os.getenv("AIML_API_KEY")
9
+ API_URL = 'https://api.aimlapi.com'
10
 
11
+ # Call the standart chat completion endpoint to get an ID
12
+ def _complete_chat():
13
+ client = OpenAI(
14
+ base_url=API_URL,
15
+ api_key=API_KEY,
16
+ )
17
 
18
+ response = client.chat.completions.create(
19
+ model="bagoodex/bagoodex-search-v1",
20
+ messages=[
21
+ {
22
+ "role": "user",
23
+
24
+ # Enter your query here
25
+ "content": 'how to make a slingshot',
26
+ },
27
+ ],
28
+ )
29
+
30
+
31
+ print(response.choices[0].message.content)
32
 
 
33
 
34
+ # Run the function
35
+ # _complete_chat()
test_app.py DELETED
@@ -1,41 +0,0 @@
1
- import gradio as gr
2
- import re
3
-
4
- def embed_google_map(map_url):
5
- """
6
- Extracts a textual location from the Google Maps URL and returns
7
- an embedded Google Map iframe.
8
- """
9
- # Simple approach: try to extract "San+Francisco,+CA" from the URL
10
- # by capturing what's after '/maps/place/'
11
- match = re.search(r'/maps/place/([^/]+)', map_url)
12
- if not match:
13
- return "Invalid Google Maps URL. Could not extract location."
14
-
15
- location_text = match.group(1)
16
- # location_text might contain extra parameters, so let's just
17
- # strip everything after the first '?' or '/':
18
- location_text = re.split(r'[/?]', location_text)[0]
19
-
20
- embed_html = f"""
21
- <iframe
22
- width="600"
23
- height="450"
24
- style="border:0"
25
- loading="lazy"
26
- allowfullscreen
27
- src="https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY&q={location_text}">
28
- </iframe>
29
- """
30
- return embed_html
31
-
32
- with gr.Blocks() as demo:
33
- map_url_input = gr.Textbox(label="Enter Google Maps URL")
34
- map_output = gr.HTML(label="Embedded Google Map")
35
-
36
- map_url_input.change(embed_google_map, inputs=map_url_input, outputs=map_output)
37
-
38
- demo.launch()
39
-
40
-
41
- # <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d11988.613116381091!2d69.19850824650604!3d41.3055290002301!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x38ae8bc5b351bc23%3A0x707af898bf0cdb4d!2sEsenin%20St%2018%2C%20Tashkent%2C%20Uzbekistan!5e0!3m2!1sen!2s!4v1740469397909!5m2!1sen!2s" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>