ajlao commited on
Commit
606799e
1 Parent(s): d25fc36

Create chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +60 -0
chatbot.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def chat_with_model():
2
+ print("Welcome to the Question Answering Chatbot! (type 'exit' to quit)")
3
+ while True:
4
+ question = input("You: ")
5
+ if question.lower() == 'exit':
6
+ print("Goodbye!")
7
+ break
8
+
9
+ context = input("Context: ")
10
+ if context.lower() == 'exit':
11
+ print("Goodbye!")
12
+ break
13
+
14
+ response = question_answerer(question=question, context=context)
15
+ answer = response['answer']
16
+ score = response['score']
17
+
18
+ print(f"Bot: {answer} (confidence: {score:.2f})")
19
+
20
+ # Save chat function code in a script
21
+ with open('chatbot.py', 'w') as f:
22
+ f.write('''
23
+ from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering, pipeline
24
+
25
+ # Load tokenizer and model
26
+ tokenizer = AutoTokenizer.from_pretrained("my_awesome_qa_model")
27
+ model = TFAutoModelForQuestionAnswering.from_pretrained("my_awesome_qa_model")
28
+
29
+ # Create a pipeline for question answering
30
+ question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer)
31
+
32
+ # Define the chat function
33
+ def chat_with_model():
34
+ print("Welcome to the Question Answering Chatbot! (type 'exit' to quit)")
35
+ while True:
36
+ question = input("You: ")
37
+ if question.lower() == 'exit':
38
+ print("Goodbye!")
39
+ break
40
+
41
+ context = input("Context: ")
42
+ if context.lower() == 'exit':
43
+ print("Goodbye!")
44
+ break
45
+
46
+ response = question_answerer(question=question, context=context)
47
+ answer = response['answer']
48
+ score = response['score']
49
+
50
+ print(f"Bot: {answer} (confidence: {score:.2f})")
51
+
52
+ # Run the chat function
53
+ if __name__ == "__main__":
54
+ chat_with_model()
55
+ ''')
56
+
57
+ print("Chatbot script 'chatbot.py' has been created.")
58
+
59
+ if __name__ == "__main__":
60
+ chat_with_model()