jonathanjordan21 commited on
Commit
d55a3fa
·
verified ·
1 Parent(s): 7430de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -39
app.py CHANGED
@@ -1,43 +1,86 @@
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
  """
@@ -45,18 +88,6 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
45
  """
46
  demo = gr.ChatInterface(
47
  respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
  )
61
 
62
 
 
1
  import gradio as gr
2
+ import re
3
 
4
+ from sentence_transformers import SentenceTransformer
5
+ from sentence_transformers.util import cos_sim
6
+
7
+
8
+ codes = """001 - Vehicle Registration (New)
9
+ 002 - Vehicle Registration Renewal
10
+ 003 - Vehicle Ownership Transfer
11
+ 004 - Vehicle De-registration
12
+ 005 - Lost Registration Certificate Replacement
13
+ 006 - Address Change Update
14
+ 007 - Vehicle Data Correction
15
+ 008 - Ownership Name Correction
16
+ 009 - Vehicle Tax Payment
17
+ 010 - Late Payment Fee Processing
18
+ 011 - Vehicle Type/Specification Update
19
+ 012 - BBNKB (Transfer Fee of Vehicle Ownership)
20
+ 013 - STNK Issuance (Vehicle Registration Certificate)
21
+ 014 - STNK Renewal
22
+ 015 - Motor Vehicle Roadworthiness Inspection
23
+ 016 - Plate Number Renewal
24
+ 017 - Lost Plate Replacement
25
+ 018 - Vehicle Export Registration
26
+ 019 - Vehicle Import Registration
27
+ 020 - Fleet Vehicle Registration
28
+ 021 - Bulk Vehicle Registration Update
29
+ 022 - Vehicle Insurance Assistance
30
+ 023 - Vehicle Accident Reporting
31
+ 024 - Vehicle Usage Change Declaration (e.g., personal to commercial)
32
+ 025 - Legal Document Verification
33
+ 026 - Ownership Transfer for Inherited Vehicle
34
+ 027 - STNK Temporary Suspension
35
+ 028 - Proof of Ownership Document Update
36
+ 029 - Vehicle Ownership History Check
37
+ 030 - Vehicle Tax Recalculation Request
38
+ 031 - Tax Exemption Application (for special cases)
39
+ 032 - Deceased Owner’s Vehicle Ownership Transfer
40
+ 033 - Other/Undetected""".split("\n")
41
 
42
 
43
+ model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2')
44
+
45
  def respond(
46
  message,
47
  history: list[tuple[str, str]],
 
 
 
 
48
  ):
49
+ # messages = [{"role": "system", "content": system_message}]
50
+ pattern = r'\b([A-Z]{1,2})\s?(\d{4})\s?([A-Z]{3})\b'
51
+ matches = re.findall(pattern, message)
52
+
53
+ plate_numbers = ", ".join([f"{i}. " + " ".join(x) for i,x in enumerate(matches)])
54
+
55
+ codes_emb = model.encode(codes)
56
+ text_emb = model.encode(query)
57
+ scores = cos_sim(codes_emb, text_emb)[:,0]
58
+
59
+ request_code = codes[scores.argmax()]
60
+
61
+ return "Request code number: " + request_code[:3] + "\nRequest detail: " + request_code[6:] + "\n Plate numbers: "
62
 
63
+ # for val in history:
64
+ # if val[0]:
65
+ # messages.append({"role": "user", "content": val[0]})
66
+ # if val[1]:
67
+ # messages.append({"role": "assistant", "content": val[1]})
68
 
69
+ # messages.append({"role": "user", "content": message})
70
 
71
+ # response = ""
72
 
73
+ # for message in client.chat_completion(
74
+ # messages,
75
+ # max_tokens=max_tokens,
76
+ # stream=True,
77
+ # temperature=temperature,
78
+ # top_p=top_p,
79
+ # ):
80
+ # token = message.choices[0].delta.content
81
 
82
+ # response += token
83
+ # yield response
84
 
85
 
86
  """
 
88
  """
89
  demo = gr.ChatInterface(
90
  respond,
 
 
 
 
 
 
 
 
 
 
 
 
91
  )
92
 
93