william4416 commited on
Commit
10079ba
·
verified ·
1 Parent(s): fdbf842

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -1,31 +1,42 @@
1
  import gradio as gr
 
2
  import json
3
 
4
- qs = {}
 
 
 
5
 
6
- def app_inits():
7
- global qs
8
-
9
- # List of JSON file names
10
- filenames = ['fileone.json', 'filesecond.json', 'filethird.json', 'filefourth.json', 'filefifth.json']
 
 
 
11
 
12
- # Load data from each JSON file
13
- for filename in filenames:
14
- with open(filename, 'r') as file:
15
- data = json.load(file)
16
- qs.update(data)
17
-
18
- print("Loaded Q & A")
19
- print("JSON Files:", filenames)
20
 
21
- app_inits()
 
 
22
 
23
- def bot(text):
24
- global qs
25
-
26
- bot_message = qs.get(text, "Sorry, I couldn't find an answer to your question.")
27
- return bot_message
28
 
29
- chatbot = gr.Chatbot(bot)
 
30
 
31
- gr.Interface(fn=bot, inputs="text", outputs="text").launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import json
4
 
5
+ # Load model and tokenizer
6
+ model_name = "microsoft/DialoGPT-large"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
+ # Load JSON files
11
+ json_files = [
12
+ "fileone.json",
13
+ "filesecond.json",
14
+ "filethird.json",
15
+ "filefourth.json",
16
+ "filefifth.json"
17
+ ]
18
 
19
+ json_data = {}
20
+ for file in json_files:
21
+ with open(file, 'r') as f:
22
+ json_data.update(json.load(f))
 
 
 
 
23
 
24
+ def chatbot(input_text):
25
+ # Tokenize input text
26
+ input_ids = tokenizer.encode(input_text, return_tensors='pt')
27
 
28
+ # Generate response
29
+ response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
 
 
 
30
 
31
+ # Decode response and return
32
+ return tokenizer.decode(response_ids[0], skip_special_tokens=True)
33
 
34
+ iface = gr.Interface(
35
+ fn=chatbot,
36
+ inputs=gr.inputs.Textbox(lines=7, label="Input"),
37
+ outputs="text",
38
+ title="Chatbot",
39
+ description="An AI chatbot that can understand and respond to your queries."
40
+ )
41
+
42
+ iface.launch()