william4416 commited on
Commit
e6d1128
1 Parent(s): 743067f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -63
app.py CHANGED
@@ -1,68 +1,58 @@
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
-
12
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
13
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
14
-
15
-
16
- def predict(input, history=[]):
17
- # tokenize the new input sentence
18
- new_user_input_ids = tokenizer.encode(
19
- input + tokenizer.eos_token, return_tensors="pt"
 
 
 
20
  )
21
 
22
- # append the new user input tokens to the chat history
23
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
24
-
25
- # generate a response
26
- history = model.generate(
27
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
28
- ).tolist()
29
-
30
- # convert the tokens to text, and then split the responses into lines
31
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
32
- # print('decoded_response-->>'+str(response))
33
- response = [
34
- (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
35
- ] # convert to tuples of list
36
- # print('response-->>'+str(response))
37
- return response, history
38
-
39
- def read_json_file(file_path): #read json file test
40
- with open(file_path, 'r') as file:
41
- data = json.load(file)
42
- return data
43
-
44
- def main():
45
- # List of file names
46
- file_names = ['fileone.json', 'filesecond.json', 'filethird.json', 'filefourth.json', 'filefifth.json']
47
-
48
- # Read each JSON file and print its content
49
- for file_name in file_names:
50
- json_data = read_json_file(file_name)
51
- print(f"Contents of {file_name}:")
52
- print(json_data)
53
- print()
54
-
55
- if __name__ == "__main__":
56
- main()
57
-
58
-
59
-
60
- gr.Interface(
61
- fn=predict,
62
- title=title,
63
- description=description,
64
- examples=examples,
65
- inputs=["text", "state"],
66
- outputs=["chatbot", "state"],
67
- theme="finlaymacklon/boxy_violet",
68
- ).launch()
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import json
4
 
5
+ # Load pre-trained model and tokenizer (replace with desired model if needed)
6
+ model_name = "microsoft/DialoGPT-large"
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ # Function to process user input and generate response
11
+ def chat(message, history):
12
+ # Preprocess user input
13
+ input_ids = tokenizer(message, return_tensors="pt")["input_ids"]
14
+
15
+ # Generate response with beam search to improve fluency
16
+ generated_outputs = model.generate(
17
+ input_ids,
18
+ max_length=512, # Adjust max_length as needed for response length
19
+ num_beams=5, # Experiment with num_beams for better phrasing
20
+ no_repeat_ngram_size=2, # Prevent repetition in responses
21
+ early_stopping=True, # Stop generation if response seems complete
22
  )
23
 
24
+ # Decode generated tokens to text
25
+ response = tokenizer.batch_decode(generated_outputs, skip_special_tokens=True)[0]
26
+
27
+ # Access and process JSON files (improved structure)
28
+ json_files = {
29
+ "fileone.json": "your_key_in_fileone",
30
+ "filesecond.json": "your_key_in_filesecond",
31
+ "filethird.json": "your_key_in_filethird",
32
+ "filefourth.json": "your_key_in_filefourth",
33
+ "filefifth.json": "your_key_in_filefifth",
34
+ }
35
+
36
+ if any(word in message.lower() for word in ["file", "data", "information"]):
37
+ try:
38
+ # Find the relevant JSON file based on keywords in message
39
+ relevant_file = next(
40
+ file for file, key in json_files.items() if key.lower() in message.lower()
41
+ )
42
+ with open(relevant_file, "r") as f:
43
+ data = json.load(f)
44
+ relevant_info = data.get(json_files[relevant_file], "No relevant information found")
45
+ response += f"\nHere's some information I found in {relevant_file}: {relevant_info}"
46
+ except (FileNotFoundError, StopIteration):
47
+ response += "\nCouldn't find the requested file or information."
48
+ except json.JSONDecodeError:
49
+ response += "\nError processing the JSON data."
50
+
51
+ # Update history with current conversation (optional)
52
+ history.append([message, response])
53
+
54
+ return response
55
+
56
+ # Create Gradio interface for the chatbot
57
+ interface = gr.Interface(chat, inputs="textbox", outputs="textbox", catch_exceptions=True)
58
+ interface.launch(share=True) # Launch the Gradio app and share link