Spaces:
Build error
Build error
Created app
Browse files- README.md +6 -6
- app.py +193 -0
- gitattributes +35 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Operative Iq
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.36.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import boto3
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import math
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
import re
|
| 8 |
+
from botocore.client import Config
|
| 9 |
+
|
| 10 |
+
kb_id = os.getenv('KNOWLEDGE_BASE_ID')
|
| 11 |
+
aws_access_key = os.getenv('AWS_ACCESS_KEY_ID')
|
| 12 |
+
aws_secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
|
| 13 |
+
region = os.getenv('AWS_REGION')
|
| 14 |
+
initial_message = os.getenv('INITIAL_MESSAGE')
|
| 15 |
+
|
| 16 |
+
amazon_model_id = "amazon.titan-text-premier-v1:0"
|
| 17 |
+
|
| 18 |
+
bedrock_config = Config(connect_timeout=120, read_timeout=120, retries={'max_attempts': 0})
|
| 19 |
+
bedrock_client = boto3.client(
|
| 20 |
+
'bedrock-runtime',
|
| 21 |
+
region_name=region,
|
| 22 |
+
aws_access_key_id=aws_access_key,
|
| 23 |
+
aws_secret_access_key=aws_secret_key
|
| 24 |
+
)
|
| 25 |
+
bedrock_agent_client = boto3.client(
|
| 26 |
+
"bedrock-agent-runtime",
|
| 27 |
+
config=bedrock_config,
|
| 28 |
+
region_name=region,
|
| 29 |
+
aws_access_key_id=aws_access_key,
|
| 30 |
+
aws_secret_access_key=aws_secret_key
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def retrieve(query, numberOfResults=4):
|
| 34 |
+
start_time = time.time()
|
| 35 |
+
response = bedrock_agent_client.retrieve(
|
| 36 |
+
retrievalQuery= {
|
| 37 |
+
'text': query
|
| 38 |
+
},
|
| 39 |
+
knowledgeBaseId=kb_id,
|
| 40 |
+
retrievalConfiguration= {
|
| 41 |
+
'vectorSearchConfiguration': {
|
| 42 |
+
'numberOfResults': numberOfResults,
|
| 43 |
+
'overrideSearchType': "SEMANTIC", #"HYBRID",
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
)
|
| 47 |
+
end_time = time.time()
|
| 48 |
+
retrieve_execution_time = end_time - start_time
|
| 49 |
+
|
| 50 |
+
return response['retrievalResults'], retrieve_execution_time
|
| 51 |
+
|
| 52 |
+
def get_contexts(retrievalResults):
|
| 53 |
+
contexts = ""
|
| 54 |
+
for retrievedResult in retrievalResults:
|
| 55 |
+
contexts += retrievedResult['content']['text'] + '\n'
|
| 56 |
+
return contexts
|
| 57 |
+
|
| 58 |
+
def remove_link(text):
|
| 59 |
+
pattern = r"\n\nFor more information, follow the link provided: https:\/\/knowledge\.operativeiq\.com\/articles\/\d{9}"
|
| 60 |
+
cleaned_text = re.sub(pattern, '', text)
|
| 61 |
+
return cleaned_text
|
| 62 |
+
|
| 63 |
+
def get_answer(query, history, temperature, top_p, max_token_count):
|
| 64 |
+
history = remove_link(history)
|
| 65 |
+
contexts = ""
|
| 66 |
+
article_url_text = ""
|
| 67 |
+
max_words = math.floor(max_token_count*0.75)
|
| 68 |
+
|
| 69 |
+
retrievalResults, retrieve_execution_time = retrieve(query)
|
| 70 |
+
highest_score = retrievalResults[0]['score']
|
| 71 |
+
|
| 72 |
+
if highest_score > 0.45:
|
| 73 |
+
contexts = get_contexts(retrievalResults)
|
| 74 |
+
article_url = f"https://knowledge.operativeiq.com/articles/{retrievalResults[0]['metadata'].get('article_id')}"
|
| 75 |
+
article_url_text = f"\n\nFor more information, follow the link provided: {article_url}"
|
| 76 |
+
|
| 77 |
+
PROMPT_TEMPLATE = f"""
|
| 78 |
+
System: You are an intelligent assistant helping users understand and navigate website functionalities.
|
| 79 |
+
Your goal is to provide clear, accurate, and contextually relevant answers based on the information provided.
|
| 80 |
+
|
| 81 |
+
Use the information enclosed in the <context> tags and refer to the conversation history in the <history> tags to answer the user's question in the <question> tags.
|
| 82 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 83 |
+
|
| 84 |
+
Your response must:
|
| 85 |
+
- Be fully formed and grammatically correct without cutting off any sentences.
|
| 86 |
+
- Complete a logical thought or sentence before stopping, ensuring the response doesn't end mid-sentence.
|
| 87 |
+
- Be clear, easy to understand, and succinct, not exceeding {max_words} words.
|
| 88 |
+
- Refer specifically to website features or actions when relevant to the user's question.
|
| 89 |
+
- Avoid providing URL links or external references.
|
| 90 |
+
|
| 91 |
+
<history>
|
| 92 |
+
{history}
|
| 93 |
+
</history>
|
| 94 |
+
|
| 95 |
+
<context>
|
| 96 |
+
{contexts}
|
| 97 |
+
</context>
|
| 98 |
+
|
| 99 |
+
<question>
|
| 100 |
+
{query}
|
| 101 |
+
</question>
|
| 102 |
+
|
| 103 |
+
Provide a detailed, concise response that fully answers the user's question.
|
| 104 |
+
Make sure all sentences of your reponse are completely formed and grammatically correct.
|
| 105 |
+
If necessary, reduce the amount of detail provided to keep the response within the word limit but still complete.
|
| 106 |
+
|
| 107 |
+
Assistant:
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
body = json.dumps({
|
| 111 |
+
"inputText": PROMPT_TEMPLATE,
|
| 112 |
+
"textGenerationConfig": {
|
| 113 |
+
"maxTokenCount": max_token_count,
|
| 114 |
+
"temperature": temperature,
|
| 115 |
+
"topP": top_p
|
| 116 |
+
}
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
kwargs = {
|
| 120 |
+
"modelId": "amazon.titan-text-premier-v1:0",
|
| 121 |
+
"contentType": "application/json",
|
| 122 |
+
"accept": "*/*",
|
| 123 |
+
"body": body
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
start_time = time.time()
|
| 127 |
+
|
| 128 |
+
response = bedrock_client.invoke_model(**kwargs)
|
| 129 |
+
|
| 130 |
+
end_time = time.time()
|
| 131 |
+
invoke_model_time = end_time - start_time
|
| 132 |
+
|
| 133 |
+
response_body = json.loads(response.get('body').read())
|
| 134 |
+
response_text = response_body['results'][0]['outputText']
|
| 135 |
+
|
| 136 |
+
response_text += article_url_text
|
| 137 |
+
|
| 138 |
+
prompt_and_time = f"""
|
| 139 |
+
Prompt:
|
| 140 |
+
{PROMPT_TEMPLATE}
|
| 141 |
+
|
| 142 |
+
Retrieve execution time: {retrieve_execution_time} seconds
|
| 143 |
+
Invoke model execution time: {invoke_model_time} seconds
|
| 144 |
+
"""
|
| 145 |
+
return response_text, prompt_and_time
|
| 146 |
+
|
| 147 |
+
def format_chat_history(chat_history):
|
| 148 |
+
prompt = ""
|
| 149 |
+
for turn in chat_history:
|
| 150 |
+
user_message, bot_message = turn
|
| 151 |
+
prompt = f"{prompt}User: {user_message}\nAssistant: {bot_message}\n"
|
| 152 |
+
return prompt
|
| 153 |
+
|
| 154 |
+
def respond(message, chat_history, temperature=0.9, top_p=0.6, max_token_count=512):
|
| 155 |
+
formatted_history = format_chat_history(chat_history)
|
| 156 |
+
chat_history.append([message, ""])
|
| 157 |
+
|
| 158 |
+
stream, prompt_and_time = get_answer(message, formatted_history, temperature, top_p, max_token_count)
|
| 159 |
+
|
| 160 |
+
for idx, text_token in enumerate(stream):
|
| 161 |
+
if idx == 0 and text_token.startswith(" "):
|
| 162 |
+
text_token = text_token[1:]
|
| 163 |
+
|
| 164 |
+
chat_history[-1][1] += text_token
|
| 165 |
+
yield "", chat_history, prompt_and_time
|
| 166 |
+
|
| 167 |
+
def clear_chat_history():
|
| 168 |
+
return '', []
|
| 169 |
+
|
| 170 |
+
def main():
|
| 171 |
+
with gr.Blocks() as demo:
|
| 172 |
+
chatbot = gr.Chatbot([[None, initial_message]], height=550)
|
| 173 |
+
|
| 174 |
+
msg = gr.Textbox(label="Question")
|
| 175 |
+
|
| 176 |
+
with gr.Accordion(label="Advanced options", open=False):
|
| 177 |
+
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1, value=0.6, step=0.1)
|
| 178 |
+
top_p = gr.Slider(label="Top P", minimum=0.1, maximum=1, value=0.5, step=0.1)
|
| 179 |
+
max_token_count = gr.Slider(label="Max token count", minimum=1, maximum=1024, value=400, step=10)
|
| 180 |
+
prompt_and_time = gr.Textbox(label="Prompt and Time", interactive=False)
|
| 181 |
+
|
| 182 |
+
btn = gr.Button("Submit")
|
| 183 |
+
clear = gr.Button("Clear console")
|
| 184 |
+
|
| 185 |
+
btn.click(respond, inputs=[msg, chatbot, temperature, top_p, max_token_count], outputs=[msg, chatbot, prompt_and_time])
|
| 186 |
+
msg.submit(respond, inputs=[msg, chatbot, temperature, top_p, max_token_count], outputs=[msg, chatbot, prompt_and_time])
|
| 187 |
+
|
| 188 |
+
clear.click(clear_chat_history, outputs=[msg, chatbot, prompt_and_time])
|
| 189 |
+
|
| 190 |
+
demo.queue().launch()
|
| 191 |
+
|
| 192 |
+
if __name__ == "__main__":
|
| 193 |
+
main()
|
gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.22.2
|
| 2 |
+
boto3==1.34.161
|
| 3 |
+
gradio==4.41.0
|
| 4 |
+
botocore==1.34.161
|