karthickg12 commited on
Commit
39e0066
1 Parent(s): fdbf048

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -26
app.py CHANGED
@@ -1,27 +1,70 @@
1
- from transformers import pipeline
2
- insurance_context = """
3
- Insurance is a contract, represented by a policy, in which an individual or entity receives financial protection against losses.
4
- Common types include life insurance, health insurance, auto insurance, and home insurance.
5
- Life insurance provides a sum of money to beneficiaries upon the insured's death, while health insurance covers medical expenses.
6
- Auto insurance offers protection against vehicle-related accidents and damages. Home insurance covers damages to one’s property.
7
- """
8
- qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
9
- def ask_question(question, context=insurance_context):
10
- # Use the model to answer the question based on the context
11
- response = qa_pipeline({
12
- 'question': question,
13
- 'context': context
14
- })
15
- return response['answer']
16
-
17
- def chat():
18
- print("Hello! I'm your insurance Q&A chatbot. Ask me anything about insurance.")
19
- while True:
20
- user_input = input("You: ")
21
 
22
- if user_input.lower() in ["exit", "quit"]:
23
- print("Thank you for using the chatbot. Goodbye!")
24
- break
25
- answer = ask_question(user_input)
26
- print("Bot:", answer)
27
- chat()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from transformers import pipeline
2
+ # insurance_context = """
3
+ # Insurance is a contract, represented by a policy, in which an individual or entity receives financial protection against losses.
4
+ # Common types include life insurance, health insurance, auto insurance, and home insurance.
5
+ # Life insurance provides a sum of money to beneficiaries upon the insured's death, while health insurance covers medical expenses.
6
+ # Auto insurance offers protection against vehicle-related accidents and damages. Home insurance covers damages to one’s property.
7
+ # """
8
+ # qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
9
+ # def ask_question(question, context=insurance_context):
10
+ # # Use the model to answer the question based on the context
11
+ # response = qa_pipeline({
12
+ # 'question': question,
13
+ # 'context': context
14
+ # })
15
+ # return response['answer']
16
+
17
+ # def chat():
18
+ # print("Hello! I'm your insurance Q&A chatbot. Ask me anything about insurance.")
19
+ # while True:
20
+ # user_input = input("You: ")
21
 
22
+ # if user_input.lower() in ["exit", "quit"]:
23
+ # print("Thank you for using the chatbot. Goodbye!")
24
+ # break
25
+ # answer = ask_question(user_input)
26
+ # print("Bot:", answer)
27
+ # chat()
28
+
29
+
30
+ import streamlit as st
31
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
32
+
33
+ # Load BlenderBot model and tokenizer
34
+ model_name = "facebook/blenderbot-400M-distill"
35
+ tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
36
+ model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
37
+
38
+ # Function to generate a response from BlenderBot
39
+ def get_blenderbot_response(input_text):
40
+ inputs = tokenizer(input_text, return_tensors="pt")
41
+ reply_ids = model.generate(**inputs)
42
+ response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
43
+ return response
44
+
45
+ # Streamlit app
46
+ st.title("Insurance Q&A Chatbot")
47
+ st.write("Ask any question about insurance, and I'll do my best to help!")
48
+
49
+ # Chat history
50
+ if "history" not in st.session_state:
51
+ st.session_state.history = []
52
+
53
+ # Input text box for user
54
+ user_input = st.text_input("You:", "")
55
+
56
+ # Respond to user input
57
+ if user_input:
58
+ # Add user question to history
59
+ st.session_state.history.append({"user": user_input})
60
+
61
+ # Generate bot response
62
+ response = get_blenderbot_response(user_input)
63
+ st.session_state.history.append({"bot": response})
64
+
65
+ # Display chat history
66
+ for message in st.session_state.history:
67
+ if "user" in message:
68
+ st.write("**You:**", message["user"])
69
+ if "bot" in message:
70
+ st.write("**Bot:**", message["bot"])