Spaces:
Sleeping
Sleeping
1st commit!
Browse files- app.py +136 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import json
|
7 |
+
from dotenv import load_dotenv, find_dotenv
|
8 |
+
_ = load_dotenv(find_dotenv())
|
9 |
+
|
10 |
+
from predibase import Predibase, FinetuningConfig, DeploymentConfig
|
11 |
+
|
12 |
+
# Get a KEY from https://app.predibase.com/
|
13 |
+
api_token = os.getenv('PREDIBASE_API_KEY')
|
14 |
+
pb = Predibase(api_token=api_token)
|
15 |
+
|
16 |
+
adapter_id = 'tour-assistant-model/15'
|
17 |
+
lorax_client = pb.deployments.client("solar-1-mini-chat-240612")
|
18 |
+
|
19 |
+
|
20 |
+
def extract_json(gen_text, n_shot_learning=0):
|
21 |
+
if(n_shot_learning == -1) :
|
22 |
+
start_index = 0
|
23 |
+
else :
|
24 |
+
start_index = gen_text.index("### Response:\n{") + 14
|
25 |
+
if(n_shot_learning > 0) :
|
26 |
+
for i in range(0, n_shot_learning):
|
27 |
+
gen_text = gen_text[start_index:]
|
28 |
+
start_index = gen_text.index("### Response:\n{") + 14
|
29 |
+
end_index = gen_text.find("}\n\n### ") + 1
|
30 |
+
return gen_text[start_index:end_index]
|
31 |
+
|
32 |
+
def get_completion(prompt):
|
33 |
+
return lorax_client.generate(prompt, adapter_id=adapter_id, max_new_tokens=1000).generated_text
|
34 |
+
|
35 |
+
def greet(input):
|
36 |
+
sys_str = "You are a helpful support assistant. Answer the following question."
|
37 |
+
qa_list = []
|
38 |
+
n_prompt_list = []
|
39 |
+
# qa_list.append({
|
40 |
+
# "question": "What are the benefits of joining a union?",
|
41 |
+
# "answer": "Collective bargaining of salary."
|
42 |
+
# })
|
43 |
+
#
|
44 |
+
# qa_list.append({
|
45 |
+
# "question": "How much are union dues, and what do they cover?",
|
46 |
+
# "answer": "The union dues for our union is 3%."
|
47 |
+
# })
|
48 |
+
#
|
49 |
+
# qa_list.append({
|
50 |
+
# "question": "How does the union handle grievances and disputes?",
|
51 |
+
# "answer": "There will be a panel to oversee disputes"
|
52 |
+
# })
|
53 |
+
#
|
54 |
+
# qa_list.append({
|
55 |
+
# "question": "Will joining a union affect my job security?",
|
56 |
+
# "answer": "No."
|
57 |
+
# })
|
58 |
+
#
|
59 |
+
# qa_list.append({
|
60 |
+
# "question": "What is the process for joining a union?",
|
61 |
+
# "answer": "Please use the contact form."
|
62 |
+
# })
|
63 |
+
#
|
64 |
+
# qa_list.append({
|
65 |
+
# "question": "How do unions negotiate contracts with employers?",
|
66 |
+
# "answer": "Our dear leader will handle the negotiations."
|
67 |
+
# })
|
68 |
+
#
|
69 |
+
# qa_list.append({
|
70 |
+
# "question": "What role do I play as a union member?",
|
71 |
+
# "answer": "You will be invited to our monthly picnics"
|
72 |
+
# })
|
73 |
+
#
|
74 |
+
# qa_list.append({
|
75 |
+
# "question": "How do unions ensure that employers comply with agreements?",
|
76 |
+
# "answer": "We will have a monthly meeting for members"
|
77 |
+
# })
|
78 |
+
#
|
79 |
+
# qa_list.append({
|
80 |
+
# "question": "Can I be forced to join a union?",
|
81 |
+
# "answer": "What kind of questions is that! Of course no!"
|
82 |
+
# })
|
83 |
+
#
|
84 |
+
# qa_list.append({
|
85 |
+
# "question": "What happens if I disagree with the union’s decisions?",
|
86 |
+
# "answer": "We will agree to disagree"
|
87 |
+
# })
|
88 |
+
|
89 |
+
for qna in qa_list:
|
90 |
+
ques_str = qna["question"]
|
91 |
+
ans_str = qna["answer"]
|
92 |
+
n_prompt_list.append(f"""
|
93 |
+
<|im_start|>system\n{sys_str}<|im_end|>
|
94 |
+
<|im_start|>question\n{ques_str}<|im_end|>
|
95 |
+
<|im_start|>answer\n{ans_str}<|im_end|>
|
96 |
+
"""
|
97 |
+
)
|
98 |
+
|
99 |
+
n_prompt_str = "\n"
|
100 |
+
|
101 |
+
for prompt in n_prompt_list:
|
102 |
+
n_prompt_str = n_prompt_str + prompt + "\n"
|
103 |
+
|
104 |
+
total_prompt=f"""
|
105 |
+
{n_prompt_str}
|
106 |
+
|
107 |
+
<|im_start|>system\n{sys_str}<|im_end|>
|
108 |
+
<|im_start|>question
|
109 |
+
{input}\n<|im_end|>
|
110 |
+
<|im_start|>answer
|
111 |
+
"""
|
112 |
+
|
113 |
+
|
114 |
+
print("***total_prompt:")
|
115 |
+
print(total_prompt)
|
116 |
+
response = get_completion(total_prompt)
|
117 |
+
#gen_text = response["predictions"][0]["generated_text"]
|
118 |
+
#return json.dumps(extract_json(gen_text, 3))
|
119 |
+
|
120 |
+
###gen_text = response["choices"][0]["text"]
|
121 |
+
|
122 |
+
#return gen_text
|
123 |
+
|
124 |
+
###return json.dumps(extract_json(gen_text, -1))
|
125 |
+
return response
|
126 |
+
|
127 |
+
#return json.dumps(response)
|
128 |
+
|
129 |
+
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
130 |
+
#iface.launch()
|
131 |
+
|
132 |
+
#iface = gr.Interface(fn=greet, inputs=[gr.Textbox(label="Text to find entities", lines=2)], outputs=[gr.HighlightedText(label="Text with entities")], title="NER with dslim/bert-base-NER", description="Find entities using the `dslim/bert-base-NER` model under the hood!", allow_flagging="never", examples=["My name is Andrew and I live in California", "My name is Poli and work at HuggingFace"])
|
133 |
+
#iface = gr.Interface(fn=greet, inputs=[gr.Textbox(label="Question", lines=3)], outputs="json")
|
134 |
+
iface = gr.Interface(fn=greet, inputs=[gr.Textbox(label="Question", lines=3)], outputs="text")
|
135 |
+
iface.queue(api_open=True);
|
136 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
python-dotenv
|
3 |
+
predibase
|