Arjs commited on
Commit
c928c87
·
verified ·
1 Parent(s): 358ca48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -56
app.py CHANGED
@@ -1,61 +1,94 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  demo = gr.ChatInterface(
46
  respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
  )
60
 
61
 
 
1
+ import os
2
+ import getpass
3
+ from langchain_core.chat_history import(
4
+ BaseChatMessageHistory,
5
+ InMemoryChatMessageHistory,
6
+ )
7
+ from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholder
8
+ from langchain_core.runnables.history import RunnableWithMessageHistory
9
+ from langchain_core.messages import HumanMessage
10
+ from langchain_groq import ChatGroq
11
+
12
+
13
+ os.environ["GROQ_API_KEY"]='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'
14
+ model=ChatGroq(model="llama3-8b-8192")
15
+
16
+
17
+ store={}
18
+
19
+ def session_history(session_id:str)->BaseChatMessageHistory:
20
+ if session_id not in store:
21
+ store[session_id]=InMemoryChatMessageHistory()
22
+ return store[session_id]
23
+
24
+ with_message_history=RunnableWithMessageHistory(model,session_history)
25
+
26
+ config={"configurable":{"session_id":"abc2"}}
27
+
28
+ prompt=ChatPromptTemplate.from_messages(
29
+ [
30
+ (
31
+ "system",
32
+ """Predefined Question-Answer Set:
33
+
34
+ Question: What is the process for changing a train ticket date?
35
+ Answer: To change the date of your train ticket, visit the official IRCTC website or app. Log in to your account, navigate to the "Booked Tickets" section, select the ticket you wish to modify, and choose the option to change the journey date. Please note that date changes are subject to availability and specific conditions, and additional charges may apply.
36
+
37
+ Question: How can I check the PNR status of my train ticket?
38
+ Answer: You can check the PNR status of your train ticket by visiting the IRCTC website, using the IRCTC mobile app, or sending an SMS with your PNR number to the designated railway inquiry number. Additionally, you can check the status at the railway station's inquiry counter or through various third-party apps and websites that offer PNR status checking services.
39
+
40
+ Question: What should I do if my train is delayed?
41
+ Answer: If your train is delayed, you can take the following steps: Stay informed by checking the live train status through the IRCTC website or mobile app. If the delay is significant and you wish to cancel your journey, you may be eligible for a full refund. Alternatively, if the delay is more than three hours, you can file for a refund even after the scheduled departure. Always keep your ticket and PNR number handy when seeking assistance.
42
+
43
+ Question: How do I get a refund for a canceled ticket?
44
+ Answer: To get a refund for a canceled train ticket, log in to your IRCTC account, go to the "Booked Tickets" section, and find the ticket you want to cancel. Follow the prompts to cancel the ticket. Refunds are processed according to the IRCTC refund policy, which depends on the time of cancellation relative to the train's departure. The refund amount will be credited back to the payment method used during booking.
45
+
46
+ Question: Can I modify the passenger details after booking?
47
+ Answer: Yes, you can modify certain passenger details after booking, such as the name or age of a passenger. To do this, visit a railway reservation office with a printout of the e-ticket and a valid ID proof of the passenger. Modifications must be made at least 24 hours before the train's scheduled departure. Note that only one modification per ticket is allowed, and certain conditions apply.User Query: Users will likely ask questions related to train ticket reservations, modifications, refunds, status checks, or issues they might encounter during their journey. These queries may vary in phrasing but should relate to the above predefined questions.
48
+
49
+ Question: What should I do if the food served on the train is of poor quality?
50
+ Answer: If you encounter poor-quality food on the train, you can file a complaint through the IRCTC app or website. Please provide a detailed description of the issue and attach an image of the food for verification. This will help us address the problem more effectively and ensure that appropriate actions are taken.
51
+
52
+ Question: How can I report unclean toilets or compartments during my journey?
53
+ Answer: To report unclean toilets or compartments, please use the IRCTC app or website's complaint section. Attach an image of the unclean area and provide a brief description of the issue. Our team will work to resolve the problem as soon as possible to ensure a more comfortable journey for all passengers.
54
+
55
+ Question: What steps should I take if I witness an accident or safety issue on the train?
56
+ Answer: If you witness an accident or any safety issue on the train, immediately inform the train staff or contact the railway helpline number. Additionally, you can report the incident through the IRCTC app or website. Please include a description of the event and attach any relevant images to help us respond quickly and appropriately.
57
+
58
+ Question: How do I complain about loud or disruptive passengers in my compartment?
59
+ Answer: To report loud or disruptive passengers, you can file a complaint using the IRCTC app or website. Please describe the situation in detail and, if possible, attach an image to support your complaint. The railway authorities will take the necessary actions to ensure a peaceful journey for all passengers.
60
+
61
+ Question: What can I do if I find pests or insects in my train compartment?
62
+ Answer: If you find pests or insects in your train compartment, report the issue immediately through the IRCTC app or website. Attach an image of the pests or insects and provide a brief description of the problem. This will help our cleaning staff take swift action to resolve the issue and maintain a hygienic environment on the train.
63
+
64
+ Response Template:
65
+
66
+ If the user's query directly relates to one of the predefined questions, generate a response using the relevant answer from the predefined set.
67
+ If the user's query does not relate to any of the predefined questions, respond with: "We can't handle this request."
68
+ Guidelines for Relevance:
69
+
70
+ The model should first attempt to match the user’s query to the closest predefined question. This match should consider synonyms, related terms, and common phrasing variations.
71
+ If the query matches sufficiently, generate a response using the associated answer.
72
+ If no match is found, or the query is outside the scope of the predefined questions, the model should output, "We can't handle this request.""",
73
+ ),
74
+ MessagesPlaceholder("messages"),
75
+ ]
76
+ )
77
+
78
+
79
+ chain=prompt | model
80
+
81
+
82
+ def respond(message):
83
+ query=input("Enter your query:")
84
+ response=chain.invoke(
85
+ [HumanMessage(content=query)],
86
+ config=config,
87
+ )
88
+ print(response.content)
89
+
90
  demo = gr.ChatInterface(
91
  respond,
 
 
 
 
 
 
 
 
 
 
 
 
92
  )
93
 
94