dwojcik commited on
Commit
e2f5139
1 Parent(s): d89a3b0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # model_name = "dwojcik/gpt2-large-fine-tuned-context-256"
5
+ model_name = "gpt2-large"
6
+ model = AutoModelForCausalLM.from_pretrained(model_name)
7
+ model.generation_config.temperature = 2.0
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="right")
9
+ tokenizer.pad_token = tokenizer.eos_token
10
+
11
+ def generate_response(user_message):
12
+ inputs = tokenizer.encode(user_message, return_tensors='pt')
13
+ outputs = model.generate(inputs, max_length=150, num_return_sequences=1)
14
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
15
+ return response
16
+
17
+ def user(user_message, history):
18
+ return gr.update(value="", interactive=False), history + [[user_message, None]]
19
+
20
+ def bot(history):
21
+ user_message = history[-1][0]
22
+ bot_message = generate_response(user_message)
23
+ history[-1][1] = bot_message
24
+ return history
25
+
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("""
28
+ # GPT-PTZE
29
+ This chatbot utilizes a fine-tuned GPT-2 large model from OpenAI to generate contextually relevant responses based on user input. It was trained on large corpus of data from Przegląd Elektrotechniczny.""")
30
+ chatbot = gr.Chatbot()
31
+ msg = gr.Textbox(label="Your input")
32
+ clear = gr.Button("Clear")
33
+
34
+ response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
35
+ bot, chatbot, chatbot
36
+ )
37
+ response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
38
+ clear.click(lambda: None, None, chatbot, queue=False)
39
+
40
+ demo.queue()
41
+ demo.launch(server_name="0.0.0.0")