william4416 commited on
Commit
b50242b
·
verified ·
1 Parent(s): c0231ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+ import json
5
+
6
+
7
+ title = "AI ChatBot"
8
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
9
+ examples = [["How are you?"]]
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
12
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
13
+
14
+ def predict(input, history=[], json_file_path=''):
15
+ if json_file_path:
16
+ # Read JSON data from file
17
+ with open(json_file_path, 'r') as file:
18
+ json_data = json.load(file)
19
+
20
+ # Get prompt from JSON data
21
+ prompt = json_data["prompts"][0]["prompt"]
22
+
23
+ # Use the prompt as input
24
+ input = prompt
25
+
26
+ # tokenize the new input sentence
27
+ new_user_input_ids = tokenizer.encode(
28
+ input + tokenizer.eos_token, return_tensors="pt"
29
+ )
30
+
31
+ # append the new user input tokens to the chat history
32
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
33
+
34
+ # generate a response
35
+ history = model.generate(
36
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
37
+ ).tolist()
38
+
39
+ # convert the tokens to text, and then split the responses into lines
40
+ response = tokenizer.decode(history[0]).split("\n")
41
+ return response, history
42
+
43
+ interface = gr.Interface(
44
+ fn=predict,
45
+ inputs=["text", "state", gr.inputs.File(label="JSON file")], # Added File input for JSON file
46
+ outputs=["chatbot", "state"],
47
+ title=title,
48
+ description=description,
49
+ examples=examples,
50
+ theme="finlaymacklon/boxy_violet"
51
+ )
52
+
53
+ interface.launch()