Spaces:
Running
Running
william4416
commited on
Commit
•
af8384f
1
Parent(s):
54a06b3
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,43 @@
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
|
8 |
-
# Load the question-answering pipeline
|
9 |
-
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
10 |
-
|
11 |
-
# Load tokenizer and model for dialog generation
|
12 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
13 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# Function to generate response using dialog generation model
|
38 |
-
def generate_dialog_response(user_input):
|
39 |
-
input_text = user_input + tokenizer.eos_token
|
40 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
41 |
-
|
42 |
-
# Generate response
|
43 |
-
response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
44 |
-
response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
45 |
|
46 |
-
return response_text
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
main()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
import json
|
|
|
5 |
|
6 |
+
title = "AI ChatBot"
|
7 |
+
description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
|
8 |
+
examples = [["How are you?"]]
|
9 |
|
|
|
|
|
|
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
11 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
12 |
|
13 |
+
def predict(input, history=[], file_path=None):
|
14 |
+
if file_path:
|
15 |
+
json_data = read_json_file(file_path)
|
16 |
+
print(f"Contents of {file_path}:")
|
17 |
+
print(json_data)
|
18 |
+
print()
|
19 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
|
20 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
21 |
+
history = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id).tolist()
|
22 |
+
response = tokenizer.decode(history[0]).split("\n") # Splitting on new lines
|
23 |
+
return response[0], history
|
24 |
+
|
25 |
+
def read_json_file(file_path):
|
26 |
+
with open(file_path, 'r') as file:
|
27 |
+
data = json.load(file)
|
28 |
+
return data
|
29 |
|
30 |
+
def main():
|
31 |
+
gr.Interface(
|
32 |
+
fn=predict,
|
33 |
+
title=title,
|
34 |
+
description=description,
|
35 |
+
examples=examples,
|
36 |
+
inputs=[gr.inputs.Textbox(label="User Input"), gr.inputs.File(label="JSON File")],
|
37 |
+
outputs=["text", "text"],
|
38 |
+
theme="finlaymacklon/boxy_violet",
|
39 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
main()
|