betajuned commited on
Commit
43718f3
1 Parent(s): 224d028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -62
app.py CHANGED
@@ -1,63 +1,47 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("betajuned/GPT-2_Kombinasi4")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import gradio as gr
3
+ import torch
4
+
5
+
6
+ title = "????AI ChatBot"
7
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
8
+ examples = [["How are you?"]]
9
+
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained("betajuned/GPT-2_Kombinasi4")
12
+ model = AutoModelForCausalLM.from_pretrained("betajuned/GPT-2_Kombinasi4")
13
+
14
+
15
+ def predict(input, history=[]):
16
+ # tokenize the new input sentence
17
+ new_user_input_ids = tokenizer.encode(
18
+ input + tokenizer.eos_token, return_tensors="pt"
19
+ )
20
+
21
+ # append the new user input tokens to the chat history
22
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
23
+
24
+ # generate a response
25
+ history = model.generate(
26
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
27
+ ).tolist()
28
+
29
+ # convert the tokens to text, and then split the responses into lines
30
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
31
+ # print('decoded_response-->>'+str(response))
32
+ response = [
33
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
34
+ ] # convert to tuples of list
35
+ # print('response-->>'+str(response))
36
+ return response, history
37
+
38
+
39
+ gr.Interface(
40
+ fn=predict,
41
+ title=title,
42
+ description=description,
43
+ examples=examples,
44
+ inputs=["text", "state"],
45
+ outputs=["chatbot", "state"],
46
+ theme="finlaymacklon/boxy_violet",
47
+ ).launch()