Ashmi Banerjee commited on
Commit
6a3b6f1
1 Parent(s): a99262b

updates to examples

Browse files
README.md CHANGED
@@ -21,8 +21,8 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
21
 
22
  - [x] Fix it for the new models e.g. Llama and others
23
 
24
- - [ ] Add the space secrets to have it running online
25
- - [ ] Fix the google application json file
26
 
27
  - [ ] Make the space public
28
 
@@ -32,4 +32,5 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
32
  - [ ] Adapt the gradio examples to the right format
33
  - [x] UI refactoring
34
  - [ ] Log the different queries and the results
35
- - [x] Clear does not clear the country
 
 
21
 
22
  - [x] Fix it for the new models e.g. Llama and others
23
 
24
+ - [x] Add the space secrets to have it running online
25
+ - [x] Fix the google application json file
26
 
27
  - [ ] Make the space public
28
 
 
32
  - [ ] Adapt the gradio examples to the right format
33
  - [x] UI refactoring
34
  - [ ] Log the different queries and the results
35
+ - [x] Clear does not clear the country
36
+ - [ ] Text formatting of the output
app.py CHANGED
@@ -6,6 +6,7 @@ from src.ui.components.inputs import main_component
6
  from src.ui.components.static import load_buttons, load_examples, model_settings
7
  from src.ui.setup import load_html_from_file
8
  from src.text_generation.vertexai_setup import initialize_vertexai_params
 
9
  sys.path.append("./src")
10
 
11
 
@@ -17,21 +18,22 @@ def create_ui():
17
  # Create the Gradio HTML component
18
  html_content = load_html_from_file(html_file_path)
19
 
20
- with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.green, secondary_hue=gr.themes.colors.blue)) as app:
 
21
  gr.HTML(html_content)
22
  country, starting_point, query, sustainable, model = main_component()
23
  output = gr.Textbox(label="Generated Results", lines=4)
24
  max_new_tokens, temperature = model_settings()
25
 
26
  # Load the buttons for the interface
27
- load_buttons(query, model,
28
- sustainable, country, starting_point,
29
  max_new_tokens, temperature,
30
  output,
31
  generate_text_fn=generate_text,
32
  clear_fn=clear)
33
  # Load the examples for the interface
34
- # load_examples(country, starting_point, query, model, sustainable, output, generate_text)
 
35
  return app
36
 
37
 
 
6
  from src.ui.components.static import load_buttons, load_examples, model_settings
7
  from src.ui.setup import load_html_from_file
8
  from src.text_generation.vertexai_setup import initialize_vertexai_params
9
+
10
  sys.path.append("./src")
11
 
12
 
 
18
  # Create the Gradio HTML component
19
  html_content = load_html_from_file(html_file_path)
20
 
21
+ with gr.Blocks(
22
+ theme=gr.themes.Default(primary_hue=gr.themes.colors.green, secondary_hue=gr.themes.colors.blue)) as app:
23
  gr.HTML(html_content)
24
  country, starting_point, query, sustainable, model = main_component()
25
  output = gr.Textbox(label="Generated Results", lines=4)
26
  max_new_tokens, temperature = model_settings()
27
 
28
  # Load the buttons for the interface
29
+ load_buttons(country, starting_point, query, sustainable, model,
 
30
  max_new_tokens, temperature,
31
  output,
32
  generate_text_fn=generate_text,
33
  clear_fn=clear)
34
  # Load the examples for the interface
35
+ load_examples(country, starting_point, query, sustainable, model, output, generate_text)
36
+
37
  return app
38
 
39
 
src/ui/components/actions.py CHANGED
@@ -7,10 +7,12 @@ def clear():
7
  return None, None, None, None, None, None
8
 
9
 
10
- def generate_text(query_text: str,
11
- model_name: Optional[str],
12
- is_sustainable: Optional[bool],
13
  starting_point: Optional[str],
 
 
 
14
  max_tokens: Optional[int] = 1024,
15
  temp: Optional[float] = 0.49,
16
  ):
@@ -20,7 +22,7 @@ def generate_text(query_text: str,
20
  }
21
  pipeline_response = pipeline(
22
  query=query_text,
23
- model_name=model_name,
24
  sustainability=is_sustainable,
25
  starting_point=starting_point,
26
  **model_params
 
7
  return None, None, None, None, None, None
8
 
9
 
10
+ def generate_text(
11
+ country: Optional[str],
 
12
  starting_point: Optional[str],
13
+ query_text: str,
14
+ is_sustainable: Optional[bool],
15
+ model: Optional[str],
16
  max_tokens: Optional[int] = 1024,
17
  temp: Optional[float] = 0.49,
18
  ):
 
22
  }
23
  pipeline_response = pipeline(
24
  query=query_text,
25
+ model_name=model,
26
  sustainability=is_sustainable,
27
  starting_point=starting_point,
28
  **model_params
src/ui/components/inputs.py CHANGED
@@ -1,4 +1,4 @@
1
- from typing import Tuple
2
 
3
  import gradio as gr
4
 
@@ -12,11 +12,10 @@ def get_places():
12
  df = df.sort_values(by=['country', 'city'])
13
  return df
14
 
15
-
16
- # Function to update the list of cities based on the selected country
17
  def update_cities(selected_country, df):
 
18
  filtered_cities = df[df['country'] == selected_country]['city'].tolist()
19
- return gr.Dropdown(choices=filtered_cities, interactive=True) # Make it interactive as it is not by default
20
 
21
 
22
  def main_component() -> Tuple[gr.Dropdown, gr.Dropdown, gr.Textbox, gr.Checkbox, gr.Dropdown]:
@@ -35,15 +34,17 @@ def main_component() -> Tuple[gr.Dropdown, gr.Dropdown, gr.Textbox, gr.Checkbox,
35
  country_names = list(df.country.unique())
36
  with gr.Group():
37
  # Country selection dropdown
38
- countries = gr.Dropdown(choices=country_names, multiselect=False, label="Country")
39
 
40
  # Starting point selection dropdown
41
- starting_point = gr.Dropdown(choices=[], multiselect=False, label="Select your starting point for the trip!")
 
 
42
 
43
  # When a country is selected, update the starting point options
44
- countries.select(
45
  fn=lambda selected_country: update_cities(selected_country, df),
46
- inputs=countries,
47
  outputs=starting_point
48
  )
49
 
@@ -66,4 +67,4 @@ def main_component() -> Tuple[gr.Dropdown, gr.Dropdown, gr.Textbox, gr.Checkbox,
66
  )
67
 
68
  # Return all the components individually
69
- return countries, starting_point, query, sustainable, model
 
1
+ from typing import Tuple, Optional
2
 
3
  import gradio as gr
4
 
 
12
  df = df.sort_values(by=['country', 'city'])
13
  return df
14
 
 
 
15
  def update_cities(selected_country, df):
16
+ # Filter cities based on the selected country
17
  filtered_cities = df[df['country'] == selected_country]['city'].tolist()
18
+ return gr.Dropdown(choices=filtered_cities, interactive=True)
19
 
20
 
21
  def main_component() -> Tuple[gr.Dropdown, gr.Dropdown, gr.Textbox, gr.Checkbox, gr.Dropdown]:
 
34
  country_names = list(df.country.unique())
35
  with gr.Group():
36
  # Country selection dropdown
37
+ country = gr.Dropdown(choices=country_names, multiselect=False, label="Country")
38
 
39
  # Starting point selection dropdown
40
+ starting_point = gr.Dropdown(choices=[], multiselect=False,
41
+ label="City",
42
+ placeholder="Select a city as your starting point.")
43
 
44
  # When a country is selected, update the starting point options
45
+ country.select(
46
  fn=lambda selected_country: update_cities(selected_country, df),
47
+ inputs=country,
48
  outputs=starting_point
49
  )
50
 
 
67
  )
68
 
69
  # Return all the components individually
70
+ return country, starting_point, query, sustainable, model
src/ui/components/static.py CHANGED
@@ -9,55 +9,49 @@ def load_examples(
9
  country: gr.components.Component,
10
  starting_point: gr.components.Component,
11
  query: gr.components.Component,
 
12
  model: gr.components.Component,
13
- is_sustainable: gr.components.Component,
14
  output: gr.components.Component,
15
  generate_text_fn: Callable[
16
  [str, Optional[str], Optional[bool], Optional[int], Optional[float], Optional[str]],
17
  str],
18
  ) -> gr.Examples:
19
-
20
  df = get_places()
21
- country_names = list(df.country.unique())
22
-
23
- # Example data
24
- country_example = "USA"
25
- starting_point_example = "New York"
26
- query_example = "What are some top tourist attractions?"
27
- sustainable_example = True
28
- model_example = "GPT-4"
29
-
30
- # Update the starting point options based on the example country
31
- starting_point_choices = update_cities(country_example, df)
32
-
33
- # Set the choices and default value for the starting point dropdown
34
- starting_point.choices = starting_point_choices
35
- starting_point.value = starting_point_example
36
-
37
  # Provide examples
38
- examples = [
39
- [country_example, starting_point_example, query_example, sustainable_example, model_example]
 
40
  ]
 
 
 
 
 
 
 
 
41
 
42
- # Create a Gradio Examples component
43
- gr.Examples(
44
- examples=examples,
45
- inputs=[country, starting_point, query, is_sustainable, model],
46
- outputs=[]
47
  )
48
 
 
 
49
 
50
  def load_buttons(
 
 
51
  query: gr.components.Component,
52
  model: gr.components.Component,
53
  sustainable: gr.components.Component,
54
- country: gr.components.Component,
55
- starting_point: gr.components.Component,
56
  max_new_tokens: gr.components.Component,
57
  temperature: gr.components.Component,
58
  output: gr.components.Component,
59
  generate_text_fn: Callable[
60
- [str, Optional[str], Optional[bool], Optional[int], Optional[float], Optional[str]],
61
  str],
62
  clear_fn: Callable[[], None]
63
  ) -> gr.Group:
@@ -75,6 +69,9 @@ def load_buttons(
75
 
76
  Returns:
77
  Gradio Group component containing the buttons.
 
 
 
78
  """
79
  with gr.Group() as btns:
80
  with gr.Row():
 
9
  country: gr.components.Component,
10
  starting_point: gr.components.Component,
11
  query: gr.components.Component,
12
+ sustainable: gr.components.Component,
13
  model: gr.components.Component,
 
14
  output: gr.components.Component,
15
  generate_text_fn: Callable[
16
  [str, Optional[str], Optional[bool], Optional[int], Optional[float], Optional[str]],
17
  str],
18
  ) -> gr.Examples:
19
+ # Load the places data
20
  df = get_places()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Provide examples
22
+ example_data = [
23
+ ["Austria", "Vienna", "What are some top tourist attractions?", True, "Gemini-1.0-pro"],
24
+ ["Germany", "Berlin", "Suggest a one week long itinerary for a trip in France.", False, "Gemini-1.5-Pro"],
25
  ]
26
+ eg = gr.Examples(
27
+ examples=example_data,
28
+ inputs=[country, starting_point, query, sustainable, model],
29
+ fn=generate_text_fn,
30
+ outputs=[output],
31
+ cache_examples=True,
32
+ )
33
+ print(f"Examples loaded {eg.dataset.samples}")
34
 
35
+ starting_point.change(
36
+ fn=lambda selected_country: update_cities(selected_country, df),
37
+ inputs=country,
38
+ outputs=starting_point
 
39
  )
40
 
41
+ print("Examples loaded")
42
+
43
 
44
  def load_buttons(
45
+ country: gr.components.Component,
46
+ starting_point: gr.components.Component,
47
  query: gr.components.Component,
48
  model: gr.components.Component,
49
  sustainable: gr.components.Component,
 
 
50
  max_new_tokens: gr.components.Component,
51
  temperature: gr.components.Component,
52
  output: gr.components.Component,
53
  generate_text_fn: Callable[
54
+ [Optional[str], str, Optional[str], Optional[bool], Optional[int], Optional[float]],
55
  str],
56
  clear_fn: Callable[[], None]
57
  ) -> gr.Group:
 
69
 
70
  Returns:
71
  Gradio Group component containing the buttons.
72
+ :param clear_fn:
73
+ :param generate_text_fn:
74
+ :param country:
75
  """
76
  with gr.Group() as btns:
77
  with gr.Row():