Spaces:
Build error
Build error
Clean and restructure the module
Browse files- clarifai_grpc_helper.py +50 -46
clarifai_grpc_helper.py
CHANGED
|
@@ -1,67 +1,71 @@
|
|
| 1 |
-
from global_config import GlobalConfig
|
| 2 |
-
|
| 3 |
-
######################################################################################################
|
| 4 |
-
# In this section, we set the user authentication, user and app ID, model details, and the URL of
|
| 5 |
-
# the text we want as an input. Change these strings to run your own example.
|
| 6 |
-
######################################################################################################
|
| 7 |
-
|
| 8 |
-
TEXT_FILE_URL = 'https://samples.clarifai.com/negative_sentence_12.txt'
|
| 9 |
-
|
| 10 |
-
############################################################################
|
| 11 |
-
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
|
| 12 |
-
############################################################################
|
| 13 |
-
|
| 14 |
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
|
| 15 |
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
|
| 16 |
from clarifai_grpc.grpc.api.status import status_code_pb2
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
('authorization', 'Key ' + GlobalConfig.CLARIFAI_PAT),
|
| 23 |
-
# ('temp', '0.9'), # Does not work
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
user_id=GlobalConfig.CLARIFAI_USER_ID,
|
| 28 |
app_id=GlobalConfig.CLARIFAI_APP_ID
|
| 29 |
)
|
| 30 |
|
| 31 |
-
RAW_TEXT = '''You are a helpful, intelligent chatbot.
|
| 32 |
-
Create the slides for a presentation on the given topic.
|
| 33 |
-
Include main headings for each slide, detailed bullet points for each slide.
|
| 34 |
-
Add relevant content to each slide.
|
| 35 |
-
The output should be complete, coherent, and have maximum 255 tokens.
|
| 36 |
|
| 37 |
Topic:
|
| 38 |
Talk about AI, covering what it is and how it works. Add its pros, cons, and future prospects. Also, cover its job prospects.
|
| 39 |
'''
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
print(
|
| 67 |
-
print(output.data.text.raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
|
| 2 |
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
|
| 3 |
from clarifai_grpc.grpc.api.status import status_code_pb2
|
| 4 |
|
| 5 |
+
from global_config import GlobalConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
CHANNEL = ClarifaiChannel.get_grpc_channel()
|
| 9 |
+
STUB = service_pb2_grpc.V2Stub(CHANNEL)
|
| 10 |
|
| 11 |
+
METADATA = (
|
| 12 |
('authorization', 'Key ' + GlobalConfig.CLARIFAI_PAT),
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
+
USER_DATA_OBJECT = resources_pb2.UserAppIDSet(
|
| 16 |
user_id=GlobalConfig.CLARIFAI_USER_ID,
|
| 17 |
app_id=GlobalConfig.CLARIFAI_APP_ID
|
| 18 |
)
|
| 19 |
|
| 20 |
+
RAW_TEXT = '''You are a helpful, intelligent chatbot. Create the slides for a presentation on the given topic. Include main headings for each slide, detailed bullet points for each slide. Add relevant content to each slide. Do not output any blank line.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
Topic:
|
| 23 |
Talk about AI, covering what it is and how it works. Add its pros, cons, and future prospects. Also, cover its job prospects.
|
| 24 |
'''
|
| 25 |
|
| 26 |
+
|
| 27 |
+
def get_text_from_llm(prompt: str) -> str:
|
| 28 |
+
post_model_outputs_response = STUB.PostModelOutputs(
|
| 29 |
+
service_pb2.PostModelOutputsRequest(
|
| 30 |
+
user_app_id=USER_DATA_OBJECT, # The userDataObject is created in the overview and is required when using a PAT
|
| 31 |
+
model_id=GlobalConfig.CLARIFAI_MODEL_ID,
|
| 32 |
+
# version_id=MODEL_VERSION_ID, # This is optional. Defaults to the latest model version
|
| 33 |
+
inputs=[
|
| 34 |
+
resources_pb2.Input(
|
| 35 |
+
data=resources_pb2.Data(
|
| 36 |
+
text=resources_pb2.Text(
|
| 37 |
+
raw=prompt
|
| 38 |
+
)
|
| 39 |
)
|
| 40 |
)
|
| 41 |
+
]
|
| 42 |
+
),
|
| 43 |
+
metadata=METADATA
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
|
| 47 |
+
print(post_model_outputs_response.status)
|
| 48 |
+
raise Exception(f"Post model outputs failed, status: {post_model_outputs_response.status.description}")
|
| 49 |
+
|
| 50 |
+
# Since we have one input, one output will exist here
|
| 51 |
+
output = post_model_outputs_response.outputs[0]
|
| 52 |
+
|
| 53 |
+
# print("Completion:\n")
|
| 54 |
+
# print(output.data.text.raw)
|
| 55 |
+
|
| 56 |
+
return output.data.text.raw
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
topic = ('Talk about AI, covering what it is and how it works.'
|
| 61 |
+
' Add its pros, cons, and future prospects.'
|
| 62 |
+
' Also, cover its job prospects.'
|
| 63 |
+
)
|
| 64 |
+
print(topic)
|
| 65 |
|
| 66 |
+
with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r') as in_file:
|
| 67 |
+
prompt_txt = in_file.read()
|
| 68 |
+
prompt_txt = prompt_txt.replace('{topic}', topic)
|
| 69 |
+
response_txt = get_text_from_llm(prompt_txt)
|
| 70 |
|
| 71 |
+
print('Output:\n', response_txt)
|
|
|