Deva1211 commited on
Commit
9a3c26a
·
1 Parent(s): 12a6b31

Created all required files

Browse files
Files changed (2) hide show
  1. app.py +55 -54
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,64 +1,65 @@
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("HuggingFaceH4/zephyr-7b-beta")
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
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
1
+ # app.py
 
 
 
 
 
 
2
 
3
+ import gradio as gr
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
 
7
+ # Load the tokenizer and model
8
+ # Using a specific revision to ensure compatibility
9
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
10
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Define the prediction function
13
+ def predict(message, history):
14
+ # 'history' is a list of lists, where each inner list has a user and a bot message.
15
+ # We need to format it for DialoGPT.
16
+ history_transformer_format = []
17
+ for user, bot in history:
18
+ history_transformer_format.append(user)
19
+ history_transformer_format.append(bot)
20
 
21
+ # Join the history and the new message, separated by the EOS token
22
+ history_string = "".join(history_transformer_format)
23
+ input_text = history_string + message + tokenizer.eos_token
24
 
25
+ # Tokenize the input
26
+ new_user_input_ids = tokenizer.encode(input_text, return_tensors='pt')
 
 
 
 
 
 
27
 
28
+ # Generate a response
29
+ # The max_length is set to 1250 to allow for a decent conversation history.
30
+ bot_output_ids = model.generate(
31
+ new_user_input_ids,
32
+ max_length=1250,
33
+ pad_token_id=tokenizer.eos_token_id,
34
+ no_repeat_ngram_size=3,
35
+ do_sample=True,
36
+ top_k=100,
37
+ top_p=0.7,
38
+ temperature=0.8
39
+ )
40
 
41
+ # Decode the response, skipping the input part
42
+ response = tokenizer.decode(bot_output_ids[:, new_user_input_ids.shape[-1]:][0], skip_special_tokens=True)
43
+
44
+ # Return an empty string to clear the textbox and the updated history
45
+ return "", history + [[message, response]]
46
 
47
+ # Build the Gradio interface
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("## DialoGPT-medium Chatbot")
50
+ gr.Markdown("This chatbot uses the microsoft/DialoGPT-medium model. Start typing to chat!")
51
+
52
+ chatbot = gr.Chatbot()
53
+ textbox = gr.Textbox(placeholder="Type your message here and press Enter")
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ # When the user submits the textbox, call the 'predict' function
56
+ textbox.submit(
57
+ predict,
58
+ inputs=[textbox, chatbot],
59
+ outputs=[textbox, chatbot]
60
+ )
61
 
62
+ # Enable the queue for better handling of multiple users and to enable API usage
63
+ demo.queue()
64
+ # Launch the app
65
+ demo.launch()
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.25.2
 
 
 
1
+ torch
2
+ transformers
3
+ gradio