davidberenstein1957 HF staff commited on
Commit
ba0ce5d
1 Parent(s): f6a24f2

Add conversation logger to Argilla

Browse files
Files changed (5) hide show
  1. app copy.py +154 -0
  2. app.py +35 -9
  3. chat_interface_preference.py +796 -0
  4. requirements.txt +1 -0
  5. test.py +36 -0
app copy.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import os
4
+ from threading import Thread
5
+ from typing import Iterator
6
+
7
+ import gradio as gr
8
+ import spaces
9
+ import torch
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
11
+
12
+ from .chat_interface_preference import ChatInterface
13
+
14
+ from .chat_interface_preference import ChatInterface
15
+
16
+ MAX_MAX_NEW_TOKENS = 2048
17
+ DEFAULT_MAX_NEW_TOKENS = 1024
18
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
19
+
20
+ if torch.cuda.is_available():
21
+ model_id = "davidberenstein1957/ultra-feedback-dutch-cleaned-hq-spin-geitje-7b-ultra-sft_iter2"
22
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
23
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
24
+
25
+
26
+ @spaces.GPU
27
+ def generate(
28
+ message: str,
29
+ chat_history: list[tuple[str, str]],
30
+ max_new_tokens: int = 1024,
31
+ temperature: float = 0.06,
32
+ top_p: float = 0.95,
33
+ top_k: int = 40,
34
+ repetition_penalty: float = 1.2,
35
+ ) -> Iterator[str]:
36
+ conversation = []
37
+ for user, assistant in chat_history:
38
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
39
+ conversation.append({"role": "user", "content": message})
40
+
41
+ input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
42
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
43
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
44
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
45
+ input_ids = input_ids.to(model.device)
46
+
47
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
48
+ generate_kwargs = dict(
49
+ {"input_ids": input_ids},
50
+ streamer=streamer,
51
+ max_new_tokens=max_new_tokens,
52
+ do_sample=True,
53
+ top_p=top_p,
54
+ top_k=top_k,
55
+ temperature=temperature,
56
+ num_beams=1,
57
+ repetition_penalty=repetition_penalty,
58
+ )
59
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
60
+ t.start()
61
+
62
+ outputs = []
63
+ for text in streamer:
64
+ outputs.append(text)
65
+ yield "".join(outputs)
66
+
67
+
68
+ chat_interface = ChatInterface(
69
+ fn=generate,
70
+ chatbot=gr.Chatbot(
71
+ height=450, label="GEITje-SPIN", show_share_button=True, avatar_images=(None, "geitje-logo.jpg")
72
+ ),
73
+ # textbox=gr.Textbox(value="Typ een bericht…"),
74
+ cache_examples=False,
75
+ additional_inputs=[
76
+ gr.Slider(
77
+ label="Max new tokens",
78
+ minimum=1,
79
+ maximum=MAX_MAX_NEW_TOKENS,
80
+ step=1,
81
+ value=DEFAULT_MAX_NEW_TOKENS,
82
+ ),
83
+ gr.Slider(
84
+ label="Temperature",
85
+ minimum=0.05,
86
+ maximum=1.2,
87
+ step=0.05,
88
+ value=0.2,
89
+ ),
90
+ gr.Slider(
91
+ label="Top-p (nucleus sampling)",
92
+ minimum=0.05,
93
+ maximum=1.0,
94
+ step=0.05,
95
+ value=0.9,
96
+ ),
97
+ gr.Slider(
98
+ label="Top-k",
99
+ minimum=1,
100
+ maximum=1000,
101
+ step=1,
102
+ value=50,
103
+ ),
104
+ gr.Slider(
105
+ label="Repetition penalty",
106
+ minimum=1.0,
107
+ maximum=2.0,
108
+ step=0.05,
109
+ value=1.2,
110
+ ),
111
+ ],
112
+ examples=[
113
+ ["""Vraagje: welk woord hoort er niet in dit rijtje thuis: "auto, vliegtuig, geit, bus"?"""],
114
+ [
115
+ "Schrijf een nieuwsbericht voor De Speld over de inzet van een kudde geiten door het Nederlands Forensisch Instituut"
116
+ ],
117
+ ["Wat zijn 3 leuke dingen om te doen als ik een weekendje naar Friesland ga?"],
118
+ ["Met wie trad clown Bassie op?"],
119
+ ["Kan je naar de maan fietsen?"],
120
+ ["Wat is het belang van open source taalmodellen?"],
121
+ [
122
+ """```
123
+ Wortelverkopers krijgen miljoenenboete voor ongeoorloofd samenspannen
124
+ Door onze economieredactie
125
+ 14 dec 2023 om 12:58
126
+ Update: 20 uur geleden
127
+ 162 reacties
128
+ Delen
129
+ Toezichthouder ACM heeft een Nederlands wortelkartel aangepakt. Vier telers en verkopers van wortelen krijgen samen ruim 2,5 miljoen euro boete vanwege ongeoorloofde afspraken over het verdelen van de markt.
130
+ Het gaat om telers en verkopers Laarakker, VanRijsingen, Veco en Verduyn. De vier bedrijven verkopen waspeen en Parijse wortelen aan conserven- en diepvriesfabrikanten in Nederland, België en Duitsland. Waspeen wordt vaak verkocht in potten of blikken in een mix met erwtjes.
131
+ De vier bedrijven hadden in 2018 afgesproken dat ze tien jaar lang niet overal de concurrentie met elkaar zouden aangaan. Zo zou Veco tien jaar lang geen waspeen telen of verkopen. Daarnaast zouden Laarakker, VanRijsingen en Verduyn juist de Parijse wortelen links laten liggen.
132
+ Ook betaalden de andere wortelverkopers Veco ter compensatie van de afspraken. Laarakker en Veco maakten ook nog afzonderlijke afspraken over de levering van Parijse wortelen aan Duitse klanten.
133
+ Zulke afspraken zijn verboden. Als concurrentie door die samenwerking achterwege blijft en er dus sprake is van een kartel, betalen kopers mogelijk een hogere prijs, stelt de ACM.
134
+ Twee van de wortelbedrijven werkten mee door meer informatie over de ongeoorloofde afspraken te delen met de toezichthouder. Daardoor kregen zij een lagere boete.
135
+ ```
136
+ Vat bovenstaand artikel samen"""
137
+ ],
138
+ ],
139
+ title="🐐🕷️ GEITje 7B Spin Iter 2 🕷️🐐",
140
+ description="""\
141
+ <a href="https://huggingface.co/davidberenstein1957/ultra-feedback-dutch-cleaned-hq-spin-geitje-7b-ultra-sft_iter2">GEITje 7B SPIN iter 2</a> is een geavanceerde versie van GEITje, verder getraind op uitgebreide chat datasets en ook op een preferentiedataset om beter te aligneren met het gedrag van een gewenste chatbot op basis van het [SPIN algoritme en code](https://github.com/argilla-io/distilabel-spin-dibt/), in dit geval gpt-4-turbo. De data is net iets anders dan de originele dataset want we hebben de schoonmaak voor UltraFeedback gebruikt van Argilla, de uiteindelijke dataset is [hier](https://huggingface.co/datasets/BramVanroy/ultra_feedback_dutch_cleaned) te vinden.
142
+ """,
143
+ submit_btn="Genereer",
144
+ stop_btn="Stop",
145
+ retry_btn="🔄 Opnieuw",
146
+ undo_btn="↩️ Ongedaan maken",
147
+ clear_btn="🗑️ Wissen",
148
+ )
149
+
150
+ with gr.Blocks(css="style.css") as demo:
151
+ chat_interface.render()
152
+
153
+ if __name__ == "__main__":
154
+ demo.queue(max_size=20).launch()
app.py CHANGED
@@ -4,11 +4,14 @@ import os
4
  from threading import Thread
5
  from typing import Iterator
6
 
 
7
  import gradio as gr
8
  import spaces
9
  import torch
10
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
11
 
 
 
12
  MAX_MAX_NEW_TOKENS = 2048
13
  DEFAULT_MAX_NEW_TOKENS = 1024
14
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
@@ -17,6 +20,24 @@ if torch.cuda.is_available():
17
  model_id = "davidberenstein1957/ultra-feedback-dutch-cleaned-hq-spin-geitje-7b-ultra-sft_iter2"
18
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
19
  tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  @spaces.GPU
@@ -61,12 +82,13 @@ def generate(
61
  yield "".join(outputs)
62
 
63
 
64
- chat_interface = gr.ChatInterface(
65
  fn=generate,
66
- chatbot=gr.Chatbot(height=450,
67
- label="GEITje-SPIN",
68
- show_share_button=True,
69
- avatar_images=(None, 'geitje-logo.jpg')),
 
70
  # textbox=gr.Textbox(value="Typ een bericht…"),
71
  cache_examples=False,
72
  additional_inputs=[
@@ -108,12 +130,15 @@ chat_interface = gr.ChatInterface(
108
  ],
109
  examples=[
110
  ["""Vraagje: welk woord hoort er niet in dit rijtje thuis: "auto, vliegtuig, geit, bus"?"""],
111
- ["Schrijf een nieuwsbericht voor De Speld over de inzet van een kudde geiten door het Nederlands Forensisch Instituut"],
 
 
112
  ["Wat zijn 3 leuke dingen om te doen als ik een weekendje naar Friesland ga?"],
113
  ["Met wie trad clown Bassie op?"],
114
  ["Kan je naar de maan fietsen?"],
115
  ["Wat is het belang van open source taalmodellen?"],
116
- ["""```
 
117
  Wortelverkopers krijgen miljoenenboete voor ongeoorloofd samenspannen
118
  Door onze economieredactie
119
  14 dec 2023 om 12:58
@@ -127,7 +152,8 @@ Ook betaalden de andere wortelverkopers Veco ter compensatie van de afspraken. L
127
  Zulke afspraken zijn verboden. Als concurrentie door die samenwerking achterwege blijft en er dus sprake is van een kartel, betalen kopers mogelijk een hogere prijs, stelt de ACM.
128
  Twee van de wortelbedrijven werkten mee door meer informatie over de ongeoorloofde afspraken te delen met de toezichthouder. Daardoor kregen zij een lagere boete.
129
  ```
130
- Vat bovenstaand artikel samen"""]
 
131
  ],
132
  title="🐐🕷️ GEITje 7B Spin Iter 2 🕷️🐐",
133
  description="""\
@@ -144,4 +170,4 @@ with gr.Blocks(css="style.css") as demo:
144
  chat_interface.render()
145
 
146
  if __name__ == "__main__":
147
- demo.queue(max_size=20).launch()
 
4
  from threading import Thread
5
  from typing import Iterator
6
 
7
+ import argilla as rg
8
  import gradio as gr
9
  import spaces
10
  import torch
11
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
12
 
13
+ from .chat_interface_preference import ChatInterface
14
+
15
  MAX_MAX_NEW_TOKENS = 2048
16
  DEFAULT_MAX_NEW_TOKENS = 1024
17
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
 
20
  model_id = "davidberenstein1957/ultra-feedback-dutch-cleaned-hq-spin-geitje-7b-ultra-sft_iter2"
21
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
22
  tokenizer = AutoTokenizer.from_pretrained(model_id)
23
+ style = "<style>.user-message,.system-message{display:flex;margin:10px}.user-message .message-content{background-color:#c2e3f7;color:#000}.system-message .message-content{background-color:#f5f5f5;color:#000}.message-content{padding:10px;border-radius:10px;max-width:70%;word-wrap:break-word}.container{display:flex;justify-content:space-between}.column{width:48%}</style>"
24
+
25
+ client = rg.Argilla(api_url="https://davidberenstein1957-argilla-gradio.hf.space", api_key="owner.apikey")
26
+
27
+ required_settings = rg.Settings(
28
+ fields=[rg.TextField(name="conversation")],
29
+ questions=[
30
+ rg.TextQuestion(name="chosen"),
31
+ rg.TextQuestion(name="rejected"),
32
+ ],
33
+ )
34
+ name = "test"
35
+ if client.datasets(name=name).exists():
36
+ dataset: rg.Dataset = client.datasets(name=name)
37
+
38
+ else:
39
+ dataset = rg.Dataset(name=name, settings=required_settings)
40
+ dataset.create()
41
 
42
 
43
  @spaces.GPU
 
82
  yield "".join(outputs)
83
 
84
 
85
+ chat_interface = ChatInterface(
86
  fn=generate,
87
+ chatbot=gr.Chatbot(
88
+ height=450, label="GEITje-SPIN", show_share_button=True, avatar_images=(None, "geitje-logo.jpg")
89
+ ),
90
+ style=style,
91
+ rg_dataset=dataset,
92
  # textbox=gr.Textbox(value="Typ een bericht…"),
93
  cache_examples=False,
94
  additional_inputs=[
 
130
  ],
131
  examples=[
132
  ["""Vraagje: welk woord hoort er niet in dit rijtje thuis: "auto, vliegtuig, geit, bus"?"""],
133
+ [
134
+ "Schrijf een nieuwsbericht voor De Speld over de inzet van een kudde geiten door het Nederlands Forensisch Instituut"
135
+ ],
136
  ["Wat zijn 3 leuke dingen om te doen als ik een weekendje naar Friesland ga?"],
137
  ["Met wie trad clown Bassie op?"],
138
  ["Kan je naar de maan fietsen?"],
139
  ["Wat is het belang van open source taalmodellen?"],
140
+ [
141
+ """```
142
  Wortelverkopers krijgen miljoenenboete voor ongeoorloofd samenspannen
143
  Door onze economieredactie
144
  14 dec 2023 om 12:58
 
152
  Zulke afspraken zijn verboden. Als concurrentie door die samenwerking achterwege blijft en er dus sprake is van een kartel, betalen kopers mogelijk een hogere prijs, stelt de ACM.
153
  Twee van de wortelbedrijven werkten mee door meer informatie over de ongeoorloofde afspraken te delen met de toezichthouder. Daardoor kregen zij een lagere boete.
154
  ```
155
+ Vat bovenstaand artikel samen"""
156
+ ],
157
  ],
158
  title="🐐🕷️ GEITje 7B Spin Iter 2 🕷️🐐",
159
  description="""\
 
170
  chat_interface.render()
171
 
172
  if __name__ == "__main__":
173
+ demo.queue(max_size=20).launch()
chat_interface_preference.py ADDED
@@ -0,0 +1,796 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This file defines a useful high-level abstraction to build Gradio chatbots: ChatInterface.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import functools
8
+ import inspect
9
+ import random
10
+ import re
11
+ from typing import AsyncGenerator, Callable, Literal, Union, cast
12
+
13
+ import anyio
14
+ from gradio.blocks import Blocks
15
+ from gradio.components import (
16
+ Button,
17
+ Chatbot,
18
+ Component,
19
+ Markdown,
20
+ MultimodalTextbox,
21
+ State,
22
+ Textbox,
23
+ get_component_instance,
24
+ )
25
+ from gradio.events import Dependency, on
26
+ from gradio.helpers import Error, Info, Warning, special_args
27
+ from gradio.helpers import create_examples as Examples # noqa: N812
28
+ from gradio.layouts import Accordion, Group, Row
29
+ from gradio.routes import Request
30
+ from gradio.themes import ThemeClass as Theme
31
+ from gradio.utils import SyncToAsyncIterator, async_iteration, async_lambda
32
+ from gradio_client.documentation import document
33
+
34
+ pattern = re.compile(r'<div class="message-identifier">(.*?)</div>', re.DOTALL)
35
+
36
+
37
+ @document()
38
+ class ChatInterface(Blocks):
39
+ """
40
+ ChatInterface is Gradio's high-level abstraction for creating chatbot UIs, and allows you to create
41
+ a web-based demo around a chatbot model in a few lines of code. Only one parameter is required: fn, which
42
+ takes a function that governs the response of the chatbot based on the user input and chat history. Additional
43
+ parameters can be used to control the appearance and behavior of the demo.
44
+
45
+ Example:
46
+ import gradio as gr
47
+
48
+ def echo(message, history):
49
+ return message
50
+
51
+ demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
52
+ demo.launch()
53
+ Demos: chatinterface_multimodal, chatinterface_random_response, chatinterface_streaming_echo
54
+ Guides: creating-a-chatbot-fast, sharing-your-app
55
+ """
56
+
57
+ def __init__(
58
+ self,
59
+ fn: Callable,
60
+ *,
61
+ multimodal: bool = False,
62
+ chatbot: Chatbot | None = None,
63
+ textbox: Textbox | MultimodalTextbox | None = None,
64
+ additional_inputs: str | Component | list[str | Component] | None = None,
65
+ additional_inputs_accordion_name: str | None = None,
66
+ additional_inputs_accordion: str | Accordion | None = None,
67
+ examples: list[str] | list[dict[str, str | list]] | list[list] | None = None,
68
+ cache_examples: bool | Literal["lazy"] | None = None,
69
+ examples_per_page: int = 10,
70
+ title: str | None = None,
71
+ description: str | None = None,
72
+ theme: Theme | str | None = None,
73
+ css: str | None = None,
74
+ js: str | None = None,
75
+ head: str | None = None,
76
+ analytics_enabled: bool | None = None,
77
+ submit_btn_one: str | None | Button = "Generate 1",
78
+ submit_btn_two: str | None | Button = "Generate 2",
79
+ submit_btn_a: str | None | Button = "Log preference 🅰️",
80
+ submit_btn_b: str | None | Button = "Log preference 🅱️",
81
+ submit_btn_ab: str | None | Button = "Random pick 🅰️=🅱️",
82
+ stop_btn: str | None | Button = "Stop",
83
+ undo_btn: str | None | Button = "↩️ Undo",
84
+ clear_btn: str | None | Button = "🗑️ Clear",
85
+ autofocus: bool = True,
86
+ concurrency_limit: int | None | Literal["default"] = "default",
87
+ fill_height: bool = True,
88
+ delete_cache: tuple[int, int] | None = None,
89
+ rg_dataset: None,
90
+ ):
91
+ """
92
+ Parameters:
93
+ fn: The function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
94
+ multimodal: If True, the chat interface will use a gr.MultimodalTextbox component for the input, which allows for the uploading of multimedia files. If False, the chat interface will use a gr.Textbox component for the input.
95
+ chatbot: An instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
96
+ textbox: An instance of the gr.Textbox or gr.MultimodalTextbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox or gr.MultimodalTextbox component will be created.
97
+ additional_inputs: An instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
98
+ additional_inputs_accordion_name: Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
99
+ additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
100
+ examples: Sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input. Should be a list of strings if `multimodal` is False, and a list of dictionaries (with keys `text` and `files`) if `multimodal` is True.
101
+ cache_examples: If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
102
+ examples_per_page: If examples are provided, how many to display per page.
103
+ title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
104
+ description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
105
+ theme: Theme to use, loaded from gradio.themes.
106
+ css: Custom css as a string or path to a css file. This css will be included in the demo webpage.
107
+ js: Custom js as a string or path to a js file. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
108
+ head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
109
+ analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
110
+ submit_btn: Text to display on the submit button. If None, no button will be displayed. If a Button object, that button will be used.
111
+ stop_btn: Text to display on the stop button, which replaces the submit_btn when the submit_btn or retry_btn is clicked and response is streaming. Clicking on the stop_btn will halt the chatbot response. If set to None, stop button functionality does not appear in the chatbot. If a Button object, that button will be used as the stop button.
112
+ retry_btn: Text to display on the retry button. If None, no button will be displayed. If a Button object, that button will be used.
113
+ undo_btn: Text to display on the delete last button. If None, no button will be displayed. If a Button object, that button will be used.
114
+ clear_btn: Text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.
115
+ autofocus: If True, autofocuses to the textbox when the page loads.
116
+ concurrency_limit: If set, this is the maximum number of chatbot submissions that can be running simultaneously. Can be set to None to mean no limit (any number of chatbot submissions can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which is 1 by default).
117
+ fill_height: If True, the chat interface will expand to the height of window.
118
+ delete_cache: A tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
119
+ """
120
+ super().__init__(
121
+ analytics_enabled=analytics_enabled,
122
+ mode="chat_interface",
123
+ css=css,
124
+ title=title or "Gradio",
125
+ theme=theme,
126
+ js=js,
127
+ head=head,
128
+ fill_height=fill_height,
129
+ delete_cache=delete_cache,
130
+ )
131
+ self.css = css
132
+ self.multimodal = multimodal
133
+ self.concurrency_limit = concurrency_limit
134
+ self.fn = fn
135
+ self.is_async = inspect.iscoroutinefunction(self.fn) or inspect.isasyncgenfunction(self.fn)
136
+ self.is_generator = inspect.isgeneratorfunction(self.fn) or inspect.isasyncgenfunction(self.fn)
137
+ self.buttons: list[Button | None] = []
138
+
139
+ self.examples = examples
140
+ self.cache_examples: bool | None | Literal["lazy"] = cache_examples
141
+ self.rg_dataset = rg_dataset
142
+
143
+ if additional_inputs:
144
+ if not isinstance(additional_inputs, list):
145
+ additional_inputs = [additional_inputs]
146
+ self.additional_inputs = [get_component_instance(i) for i in additional_inputs] # type: ignore
147
+ else:
148
+ self.additional_inputs = []
149
+ if additional_inputs_accordion_name is not None:
150
+ print(
151
+ "The `additional_inputs_accordion_name` parameter is deprecated and will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead."
152
+ )
153
+ self.additional_inputs_accordion_params = {"label": additional_inputs_accordion_name}
154
+ if additional_inputs_accordion is None:
155
+ self.additional_inputs_accordion_params = {
156
+ "label": "Additional Inputs",
157
+ "open": False,
158
+ }
159
+ elif isinstance(additional_inputs_accordion, str):
160
+ self.additional_inputs_accordion_params = {"label": additional_inputs_accordion}
161
+ elif isinstance(additional_inputs_accordion, Accordion):
162
+ self.additional_inputs_accordion_params = additional_inputs_accordion.recover_kwargs(
163
+ additional_inputs_accordion.get_config()
164
+ )
165
+ else:
166
+ raise ValueError(
167
+ f"The `additional_inputs_accordion` parameter must be a string or gr.Accordion, not {type(additional_inputs_accordion)}"
168
+ )
169
+
170
+ with self:
171
+ if title:
172
+ Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{self.title}</h1>")
173
+ if description:
174
+ Markdown(description)
175
+
176
+ if chatbot:
177
+ self.chatbot = chatbot.render()
178
+ else:
179
+ self.chatbot = Chatbot(label="Chatbot", scale=1, height=200 if fill_height else None)
180
+
181
+ with Row():
182
+ for btn in [submit_btn_a, submit_btn_b, submit_btn_ab, undo_btn, clear_btn]:
183
+ if btn is not None:
184
+ if isinstance(btn, Button):
185
+ btn.render()
186
+ elif isinstance(btn, str):
187
+ btn = Button(btn, variant="secondary", size="sm", min_width=60)
188
+ else:
189
+ raise ValueError(
190
+ f"All the _btn parameters must be a gr.Button, string, or None, not {type(btn)}"
191
+ )
192
+ self.buttons.append(btn) # type: ignore
193
+
194
+ with Group():
195
+ with Row():
196
+ if textbox:
197
+ if self.multimodal:
198
+ submit_btn_one = None
199
+ submit_btn_two = None
200
+ else:
201
+ textbox.container = False
202
+ textbox.show_label = False
203
+ textbox_ = textbox.render()
204
+ if not isinstance(textbox_, (Textbox, MultimodalTextbox)):
205
+ raise TypeError(
206
+ f"Expected a gr.Textbox or gr.MultimodalTextbox component, but got {type(textbox_)}"
207
+ )
208
+ self.textbox = textbox_
209
+ elif self.multimodal:
210
+ submit_btn_one = None
211
+ submit_btn_two = None
212
+ self.textbox = MultimodalTextbox(
213
+ show_label=False,
214
+ label="Message",
215
+ placeholder="Type a message...",
216
+ scale=7,
217
+ autofocus=autofocus,
218
+ )
219
+ else:
220
+ self.textbox = Textbox(
221
+ container=False,
222
+ show_label=False,
223
+ label="Message",
224
+ placeholder="Type a message...",
225
+ scale=7,
226
+ autofocus=autofocus,
227
+ )
228
+ submit_buttons = []
229
+ for btn in [submit_btn_one, submit_btn_two]:
230
+ if btn is not None and not multimodal:
231
+ if isinstance(btn, Button):
232
+ btn.render()
233
+ elif isinstance(btn, str):
234
+ btn = Button(
235
+ btn,
236
+ variant="primary",
237
+ scale=1,
238
+ min_width=150,
239
+ )
240
+ else:
241
+ raise ValueError(
242
+ f"The submit_btn parameter must be a gr.Button, string, or None, not {type(btn)}"
243
+ )
244
+ submit_buttons.append(btn)
245
+
246
+ if stop_btn is not None:
247
+ if isinstance(stop_btn, Button):
248
+ stop_btn.visible = False
249
+ stop_btn.render()
250
+ elif isinstance(stop_btn, str):
251
+ stop_btn = Button(
252
+ stop_btn,
253
+ variant="stop",
254
+ visible=False,
255
+ scale=1,
256
+ min_width=150,
257
+ )
258
+ else:
259
+ raise ValueError(
260
+ f"The stop_btn parameter must be a gr.Button, string, or None, not {type(stop_btn)}"
261
+ )
262
+ self.buttons.extend(submit_buttons + [stop_btn]) # type: ignore
263
+
264
+ self.fake_api_btn = Button("Fake API", visible=False)
265
+ self.fake_response_textbox = Textbox(label="Response", visible=False)
266
+ (
267
+ self.submit_btn_a,
268
+ self.submit_btn_b,
269
+ self.submit_btn_ab,
270
+ self.undo_btn,
271
+ self.clear_btn,
272
+ self.submit_btn_one,
273
+ self.submit_btn_two,
274
+ self.stop_btn,
275
+ ) = self.buttons
276
+
277
+ if examples:
278
+ if self.is_generator:
279
+ examples_fn = self._examples_stream_fn
280
+ else:
281
+ examples_fn = self._examples_fn
282
+
283
+ self.examples_handler = Examples(
284
+ examples=examples,
285
+ inputs=[self.textbox] + self.additional_inputs,
286
+ outputs=self.chatbot,
287
+ fn=examples_fn,
288
+ cache_examples=self.cache_examples,
289
+ _defer_caching=True,
290
+ examples_per_page=examples_per_page,
291
+ )
292
+
293
+ any_unrendered_inputs = any(not inp.is_rendered for inp in self.additional_inputs)
294
+ if self.additional_inputs and any_unrendered_inputs:
295
+ with Accordion(**self.additional_inputs_accordion_params): # type: ignore
296
+ for input_component in self.additional_inputs:
297
+ if not input_component.is_rendered:
298
+ input_component.render()
299
+
300
+ # The example caching must happen after the input components have rendered
301
+ if examples:
302
+ self.examples_handler._start_caching()
303
+
304
+ self.saved_input = State()
305
+ self.chatbot_state = State(self.chatbot.value) if self.chatbot.value else State([])
306
+
307
+ self._setup_events()
308
+ self._setup_api()
309
+
310
+ def _setup_events(self) -> None:
311
+ submit_fn = self._stream_fn if self.is_generator else self._submit_fn
312
+ submit_triggers = (
313
+ [self.textbox.submit, self.submit_btn_one.click] if self.submit_btn_one else [self.textbox.submit]
314
+ )
315
+ submit_fn_partial = functools.partial(submit_fn, n_generations=2)
316
+ submit_triggers_two = [self.submit_btn_two.click]
317
+ for _fn, _triggers in [(submit_fn, submit_triggers), (submit_fn_partial, submit_triggers_two)]:
318
+ submit_event = (
319
+ on(
320
+ _triggers,
321
+ self._clear_and_save_textbox,
322
+ [self.textbox],
323
+ [self.textbox, self.saved_input],
324
+ show_api=False,
325
+ queue=False,
326
+ )
327
+ .then(
328
+ self._display_input,
329
+ [self.saved_input, self.chatbot_state],
330
+ [self.chatbot, self.chatbot_state],
331
+ show_api=False,
332
+ queue=False,
333
+ )
334
+ .then(
335
+ _fn,
336
+ [self.saved_input, self.chatbot_state] + self.additional_inputs,
337
+ [self.chatbot, self.chatbot_state],
338
+ show_api=False,
339
+ concurrency_limit=cast(Union[int, Literal["default"], None], self.concurrency_limit),
340
+ )
341
+ )
342
+ self._setup_stop_events(submit_triggers, submit_event)
343
+
344
+ partial_fn_a, partial_fn_b, partial_fn_ab = (
345
+ functools.partial(self._log_fn, log="a"),
346
+ functools.partial(self._log_fn, log="b"),
347
+ functools.partial(self._log_fn, log="ab"),
348
+ )
349
+ for _fn, _btn in [
350
+ (partial_fn_a, self.submit_btn_a),
351
+ (partial_fn_b, self.submit_btn_b),
352
+ (partial_fn_ab, self.submit_btn_ab),
353
+ ]:
354
+ _btn.click(
355
+ _fn,
356
+ [self.saved_input, self.chatbot_state],
357
+ [self.chatbot, self.saved_input, self.chatbot_state],
358
+ show_api=False,
359
+ queue=True,
360
+ ).then(
361
+ async_lambda(lambda x: x),
362
+ [self.saved_input],
363
+ [self.textbox],
364
+ show_api=False,
365
+ queue=True,
366
+ )
367
+
368
+ # if self.retry_btn:
369
+ # submit_fn_partial = functools.partial(submit_fn, append=True)
370
+ # retry_event = (
371
+ # self.retry_btn.click(
372
+ # self._delete_prev_fn,
373
+ # [self.saved_input, self.chatbot_state],
374
+ # [self.chatbot, self.saved_input, self.chatbot_state],
375
+ # show_api=False,
376
+ # queue=False,
377
+ # )
378
+ # .then(
379
+ # self._display_input,
380
+ # [self.saved_input, self.chatbot_state],
381
+ # [self.chatbot, self.chatbot_state],
382
+ # show_api=False,
383
+ # queue=False,
384
+ # )
385
+ # .then(
386
+ # submit_fn_partial,
387
+ # [self.saved_input, self.chatbot_state] + self.additional_inputs,
388
+ # [self.chatbot, self.chatbot_state],
389
+ # show_api=False,
390
+ # concurrency_limit=cast(Union[int, Literal["default"], None], self.concurrency_limit),
391
+ # )
392
+ # )
393
+ # self._setup_stop_events([self.retry_btn.click], retry_event)
394
+
395
+ if self.undo_btn:
396
+ self.undo_btn.click(
397
+ self._delete_prev_fn,
398
+ [self.saved_input, self.chatbot_state],
399
+ [self.chatbot, self.saved_input, self.chatbot_state],
400
+ show_api=False,
401
+ queue=False,
402
+ ).then(
403
+ async_lambda(lambda x: x),
404
+ [self.saved_input],
405
+ [self.textbox],
406
+ show_api=False,
407
+ queue=False,
408
+ )
409
+
410
+ if self.clear_btn:
411
+ self.clear_btn.click(
412
+ async_lambda(lambda: ([], [], None)),
413
+ None,
414
+ [self.chatbot, self.chatbot_state, self.saved_input],
415
+ queue=False,
416
+ show_api=False,
417
+ )
418
+
419
+ def _setup_stop_events(self, event_triggers: list[Callable], event_to_cancel: Dependency) -> None:
420
+ if self.stop_btn and self.is_generator:
421
+ if self.submit_btn_one:
422
+ for event_trigger in event_triggers:
423
+ event_trigger(
424
+ async_lambda(
425
+ lambda: (
426
+ Button(visible=False),
427
+ Button(visible=True),
428
+ )
429
+ ),
430
+ None,
431
+ [self.submit_btn_one, self.stop_btn],
432
+ show_api=False,
433
+ queue=True,
434
+ )
435
+ event_to_cancel.then(
436
+ async_lambda(lambda: (Button(visible=True), Button(visible=False))),
437
+ None,
438
+ [self.submit_btn_one, self.stop_btn],
439
+ show_api=False,
440
+ queue=True,
441
+ )
442
+ else:
443
+ for event_trigger in event_triggers:
444
+ event_trigger(
445
+ async_lambda(lambda: Button(visible=True)),
446
+ None,
447
+ [self.stop_btn],
448
+ show_api=False,
449
+ queue=True,
450
+ )
451
+ event_to_cancel.then(
452
+ async_lambda(lambda: Button(visible=False)),
453
+ None,
454
+ [self.stop_btn],
455
+ show_api=False,
456
+ queue=True,
457
+ )
458
+ self.stop_btn.click(
459
+ None,
460
+ None,
461
+ None,
462
+ cancels=event_to_cancel,
463
+ show_api=False,
464
+ )
465
+
466
+ def _setup_api(self) -> None:
467
+ if self.is_generator:
468
+
469
+ @functools.wraps(self.fn)
470
+ async def api_fn(message, history, *args, **kwargs): # type: ignore
471
+ if self.is_async:
472
+ generator = self.fn(message, history, *args, **kwargs)
473
+ else:
474
+ generator = await anyio.to_thread.run_sync(
475
+ self.fn, message, history, *args, **kwargs, limiter=self.limiter
476
+ )
477
+ generator = SyncToAsyncIterator(generator, self.limiter)
478
+ try:
479
+ first_response = await async_iteration(generator)
480
+ yield first_response, history + [[message, first_response]]
481
+ except StopIteration:
482
+ yield None, history + [[message, None]]
483
+ async for response in generator:
484
+ yield response, history + [[message, response]]
485
+
486
+ else:
487
+
488
+ @functools.wraps(self.fn)
489
+ async def api_fn(message, history, *args, **kwargs):
490
+ if self.is_async:
491
+ response = await self.fn(message, history, *args, **kwargs)
492
+ else:
493
+ response = await anyio.to_thread.run_sync(
494
+ self.fn, message, history, *args, **kwargs, limiter=self.limiter
495
+ )
496
+ history.append([message, response])
497
+ return response, history
498
+
499
+ self.fake_api_btn.click(
500
+ api_fn,
501
+ [self.textbox, self.chatbot_state] + self.additional_inputs,
502
+ [self.textbox, self.chatbot_state],
503
+ api_name="chat",
504
+ concurrency_limit=cast(Union[int, Literal["default"], None], self.concurrency_limit),
505
+ )
506
+
507
+ def _clear_and_save_textbox(self, message: str) -> tuple[str | dict, str]:
508
+ if self.multimodal:
509
+ return {"text": "", "files": []}, message
510
+ else:
511
+ return "", message
512
+
513
+ def _append_multimodal_history(
514
+ self,
515
+ message: dict[str, list],
516
+ response: str | None,
517
+ history: list[list[str | tuple | None]],
518
+ ):
519
+ for x in message["files"]:
520
+ history.append([(x,), None])
521
+ if message["text"] is None or not isinstance(message["text"], str):
522
+ return
523
+ elif message["text"] == "" and message["files"] != []:
524
+ history.append([None, response])
525
+ else:
526
+ history.append([message["text"], response])
527
+
528
+ async def _display_input(
529
+ self, message: str | dict[str, list], history: list[list[str | tuple | None]]
530
+ ) -> tuple[list[list[str | tuple | None]], list[list[str | tuple | None]]]:
531
+ if self.multimodal and isinstance(message, dict):
532
+ self._append_multimodal_history(message, None, history)
533
+ elif isinstance(message, str):
534
+ history.append([message, None])
535
+ return history, history
536
+
537
+ def _get_conversation_from_history(self, history):
538
+ conversation = ""
539
+ history[-1] = [history[-1][0], ""]
540
+ for idx, turn in enumerate(history):
541
+ conversation += self._get_chat_message(turn[0], role="system", turn=(idx + 1))
542
+ if turn[-1]:
543
+ conversation += self._get_chat_message(turn[-1], role="user", turn=(idx + 1))
544
+
545
+ return self.css + "<body>" + conversation + "</body>"
546
+
547
+ @staticmethod
548
+ def _get_chat_message(message, role, turn):
549
+ if role == "user":
550
+ justify = "right"
551
+ else:
552
+ justify = "left"
553
+ return (
554
+ f'<div class="{role}-message" style="justify-content: {justify};">'
555
+ + '<div class="message-content">'
556
+ + f"<strong>Turn {turn} - {role.capitalize()}:</strong><br>"
557
+ + f"<em>Length: {len(message)} characters</em><br><br>"
558
+ + f'<div class="message-identifier">{message}</div>'
559
+ + "</div></div>"
560
+ )
561
+
562
+ def _get_chat_message_comparison(self, content_a, content_b):
563
+ return (
564
+ '<div class="container">'
565
+ + '<div class="column">'
566
+ + self._get_chat_message(message=content_a, role="system", turn="A")
567
+ + "</div>"
568
+ + '<div class="column">'
569
+ + self._get_chat_message(message=content_b, role="system", turn="B")
570
+ + "</div>"
571
+ + "</div>"
572
+ )
573
+
574
+ @staticmethod
575
+ def _check_if_two_responses(response):
576
+ if response:
577
+ if '<div class="message-content">' in response:
578
+ return True
579
+
580
+ async def _submit_fn(
581
+ self,
582
+ message: str | dict[str, list],
583
+ history_with_input: list[list[str | tuple | None]],
584
+ request: Request,
585
+ n_generations: int = 1,
586
+ *args,
587
+ ) -> tuple[list[list[str | tuple | None]], list[list[str | tuple | None]]]:
588
+ if not message:
589
+ Warning("Make sure to provide a message next time.")
590
+ history = history_with_input[:-1]
591
+ return history, history
592
+
593
+ _, response = history_with_input[-1]
594
+ if self._check_if_two_responses(response):
595
+ raise Error("Two options detected: undo, log or random pick continuation.")
596
+
597
+ if self.multimodal and isinstance(message, dict):
598
+ remove_input = len(message["files"]) + 1 if message["text"] is not None else len(message["files"])
599
+ history = history_with_input[:-remove_input]
600
+ else:
601
+ history = history_with_input[:-1]
602
+
603
+ inputs, _, _ = special_args(self.fn, inputs=[message, history, *args], request=request)
604
+
605
+ async def _get_response():
606
+ if self.is_async:
607
+ response = await self.fn(*inputs)
608
+ else:
609
+ response = await anyio.to_thread.run_sync(self.fn, *inputs, limiter=self.limiter)
610
+ return response
611
+
612
+ if n_generations == 1:
613
+ response = await _get_response()
614
+ else:
615
+ response_one, response_two = await _get_response(), await _get_response()
616
+ response = self._get_chat_message_comparison(response_one, response_two)
617
+
618
+ if self.multimodal and isinstance(message, dict):
619
+ self._append_multimodal_history(message, response, history)
620
+ elif isinstance(message, str):
621
+ history.append([message, response])
622
+ return history, history
623
+
624
+ async def _stream_fn(
625
+ self,
626
+ message: str | dict[str, list],
627
+ history_with_input: list[list[str | tuple | None]],
628
+ request: Request,
629
+ n_generations: int = 1,
630
+ *args,
631
+ ) -> AsyncGenerator:
632
+ _, response = history_with_input[-1]
633
+ if self._check_if_two_responses(response):
634
+ raise Error("Two options detected: undo, log or random pick continuation.")
635
+
636
+ if self.multimodal and isinstance(message, dict):
637
+ remove_input = len(message["files"]) + 1 if message["text"] is not None else len(message["files"])
638
+ history = history_with_input[:-remove_input]
639
+ else:
640
+ history = history_with_input[:-1]
641
+
642
+ inputs, _, _ = special_args(self.fn, inputs=[message, history, *args], request=request)
643
+
644
+ async def _get_response():
645
+ if self.is_async:
646
+ generator = self.fn(*inputs)
647
+ else:
648
+ generator = await anyio.to_thread.run_sync(self.fn, *inputs, limiter=self.limiter)
649
+ generator = SyncToAsyncIterator(generator, self.limiter)
650
+ return generator
651
+
652
+ generator = await _get_response()
653
+ try:
654
+ first_response = await async_iteration(generator)
655
+ if n_generations == 2:
656
+ first_response_formatted = self._get_chat_message_comparison(first_response, "")
657
+ if self.multimodal and isinstance(message, dict):
658
+ for x in message["files"]:
659
+ history.append([(x,), None])
660
+
661
+ update = history + [[message["text"], first_response_formatted]]
662
+ yield update, update
663
+ else:
664
+ update = history + [[message, first_response_formatted]]
665
+ yield update, update
666
+ except StopIteration:
667
+ if self.multimodal and isinstance(message, dict):
668
+ self._append_multimodal_history(message, None, history)
669
+ yield history, history
670
+ else:
671
+ update = history + [[message, None]]
672
+ yield update, update
673
+ async for response in generator:
674
+ if n_generations == 2:
675
+ response_formatted = self._get_chat_message_comparison(response, "")
676
+ if self.multimodal and isinstance(message, dict):
677
+ update = history + [[message["text"], response_formatted]]
678
+ yield update, update
679
+ else:
680
+ update = history + [[message, response_formatted]]
681
+ yield update, update
682
+
683
+ if n_generations == 2:
684
+ generator_two = await _get_response()
685
+ try:
686
+ first_response_two = await async_iteration(generator_two)
687
+ first_response_two = self._get_chat_message_comparison(response, first_response_two)
688
+ if self.multimodal and isinstance(message, dict):
689
+ for x in message["files"]:
690
+ history.append([(x,), None])
691
+
692
+ update = history + [[message["text"], first_response_two]]
693
+ yield update, update
694
+ else:
695
+ update = history + [[message, first_response_two]]
696
+ yield update, update
697
+ except StopIteration:
698
+ if self.multimodal and isinstance(message, dict):
699
+ self._append_multimodal_history(message, None, history)
700
+ yield history, history
701
+ else:
702
+ update = history + [[message, None]]
703
+ yield update, update
704
+ async for response_two in generator:
705
+ response_two = self._get_chat_message_comparison(response, response_two)
706
+ if self.multimodal and isinstance(message, dict):
707
+ update = history + [[message["text"], response_two]]
708
+ yield update, update
709
+ else:
710
+ update = history + [[message, response_two]]
711
+ yield update, update
712
+
713
+ async def _log_fn(
714
+ self, message: str | dict[str, list], history: list[list[str | tuple | None]], log: str
715
+ ) -> tuple[
716
+ list[list[str | tuple | None]],
717
+ str | dict[str, list],
718
+ list[list[str | tuple | None]],
719
+ ]:
720
+ if history:
721
+ prompt, response = history[-1]
722
+ if self._check_if_two_responses(response):
723
+ matches = pattern.findall(response)
724
+ option_a, option_b = matches[0], matches[1]
725
+ if log == "a":
726
+ chosen, rejected = option_a, option_b
727
+ print(Info("Logged preference: a"))
728
+ elif log == "b":
729
+ chosen, rejected = option_b, option_a
730
+ print(Info("Logged preference: b"))
731
+ elif log == "ab":
732
+ options = [option_a, option_b]
733
+ chosen, rejected = random.choice([options])
734
+ print(Info("Picked random response to continue"))
735
+
736
+ if log in ["a", "b"] and self.rg_dataset:
737
+ import argilla as rg
738
+
739
+ self.rg_dataset.records.log(
740
+ [
741
+ rg.Record(
742
+ fields={
743
+ "conversation": self._get_conversation_from_history(history),
744
+ },
745
+ suggestions=[
746
+ rg.Suggestion(question_name="chosen", value=chosen),
747
+ rg.Suggestion(question_name="rejected", value=rejected),
748
+ ],
749
+ )
750
+ ]
751
+ )
752
+ history[-1] = [prompt, chosen]
753
+ return history, message or "", history
754
+ else:
755
+ raise Error("Only one option found.")
756
+ else:
757
+ raise Error("No history found")
758
+
759
+ async def _examples_fn(self, message: str, *args) -> list[list[str | None]]:
760
+ inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
761
+ if self.is_async:
762
+ response = await self.fn(*inputs)
763
+ else:
764
+ response = await anyio.to_thread.run_sync(self.fn, *inputs, limiter=self.limiter)
765
+ return [[message, response]]
766
+
767
+ async def _examples_stream_fn(
768
+ self,
769
+ message: str,
770
+ *args,
771
+ ) -> AsyncGenerator:
772
+ inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
773
+
774
+ if self.is_async:
775
+ generator = self.fn(*inputs)
776
+ else:
777
+ generator = await anyio.to_thread.run_sync(self.fn, *inputs, limiter=self.limiter)
778
+ generator = SyncToAsyncIterator(generator, self.limiter)
779
+ async for response in generator:
780
+ yield [[message, response]]
781
+
782
+ async def _delete_prev_fn(
783
+ self,
784
+ message: str | dict[str, list],
785
+ history: list[list[str | tuple | None]],
786
+ ) -> tuple[
787
+ list[list[str | tuple | None]],
788
+ str | dict[str, list],
789
+ list[list[str | tuple | None]],
790
+ ]:
791
+ if self.multimodal and isinstance(message, dict):
792
+ remove_input = len(message["files"]) + 1 if message["text"] is not None else len(message["files"])
793
+ history = history[:-remove_input]
794
+ else:
795
+ history = history[:-1]
796
+ return history, message or "", history
requirements.txt CHANGED
@@ -6,3 +6,4 @@ sentencepiece==0.2.0
6
  spaces==0.28.3
7
  torch==2.0.1
8
  transformers==4.41.2
 
 
6
  spaces==0.28.3
7
  torch==2.0.1
8
  transformers==4.41.2
9
+ argilla==2.0.0rc1
test.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+
4
+ import argilla as rg
5
+
6
+ from chat_interface_preference import ChatInterface
7
+
8
+
9
+ def random_response(message, history, request):
10
+ response = random.choice(["Yes", "No"])
11
+ for char in response:
12
+ yield char
13
+
14
+
15
+ style = "<style>.user-message,.system-message{display:flex;margin:10px}.user-message .message-content{background-color:#c2e3f7;color:#000}.system-message .message-content{background-color:#f5f5f5;color:#000}.message-content{padding:10px;border-radius:10px;max-width:70%;word-wrap:break-word}.container{display:flex;justify-content:space-between}.column{width:48%}</style>"
16
+ client = rg.Argilla(api_url="https://davidberenstein1957-argilla-gradio.hf.space", api_key="owner.apikey")
17
+
18
+ required_settings = rg.Settings(
19
+ fields=[rg.TextField(name="conversation")],
20
+ questions=[
21
+ rg.TextQuestion(name="chosen"),
22
+ rg.TextQuestion(name="rejected"),
23
+ ],
24
+ )
25
+ name = "test"
26
+ if client.datasets(name=name).exists():
27
+ dataset: rg.Dataset = client.datasets(name=name)
28
+
29
+ else:
30
+ dataset = rg.Dataset(name=name, settings=required_settings)
31
+ dataset.create()
32
+
33
+ demo = ChatInterface(random_response, cache_examples=False, css=style, rg_dataset=dataset)
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()