Spaces:
Sleeping
Sleeping
1st commit
Browse files- app.py +67 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/14'
|
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 |
+
total_prompt=f"""
|
37 |
+
<|im_start|>system\nYou are a helpful support assistant. Answer the following question.<|im_end|>
|
38 |
+
<|im_start|>question\n How much are union dues, and what do they cover?
|
39 |
+
<|im_start|>answer\nThe union dues for our union is 3%."<|im_end|>
|
40 |
+
|
41 |
+
<|im_start|>system\nYou are a helpful support assistant. Answer the following question.<|im_end|>
|
42 |
+
<|im_start|>question
|
43 |
+
{input}. Return as a JSON response<|im_end|>
|
44 |
+
<|im_start|>answer
|
45 |
+
"""
|
46 |
+
|
47 |
+
print("***total_prompt:")
|
48 |
+
print(total_prompt)
|
49 |
+
response = get_completion(total_prompt)
|
50 |
+
#gen_text = response["predictions"][0]["generated_text"]
|
51 |
+
#return json.dumps(extract_json(gen_text, 3))
|
52 |
+
|
53 |
+
###gen_text = response["choices"][0]["text"]
|
54 |
+
|
55 |
+
#return gen_text
|
56 |
+
|
57 |
+
###return json.dumps(extract_json(gen_text, -1))
|
58 |
+
return response
|
59 |
+
|
60 |
+
#return json.dumps(response)
|
61 |
+
|
62 |
+
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
63 |
+
#iface.launch()
|
64 |
+
|
65 |
+
#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"])
|
66 |
+
iface = gr.Interface(fn=greet, inputs=[gr.Textbox(label="Question", lines=3)], outputs="json")
|
67 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
python-dotenv
|
3 |
+
predibase
|