Spaces:
Sleeping
Sleeping
william4416
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,42 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import json
|
3 |
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
qs.update(data)
|
17 |
-
|
18 |
-
print("Loaded Q & A")
|
19 |
-
print("JSON Files:", filenames)
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
bot_message = qs.get(text, "Sorry, I couldn't find an answer to your question.")
|
27 |
-
return bot_message
|
28 |
|
29 |
-
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|