barunsaha commited on
Commit
114f32d
1 Parent(s): 420199d

Clean and restructure the module

Browse files
Files changed (1) hide show
  1. 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
- channel = ClarifaiChannel.get_grpc_channel()
19
- stub = service_pb2_grpc.V2Stub(channel)
 
 
 
20
 
21
- metadata = (
22
  ('authorization', 'Key ' + GlobalConfig.CLARIFAI_PAT),
23
- # ('temp', '0.9'), # Does not work
24
  )
25
 
26
- userDataObject = resources_pb2.UserAppIDSet(
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
- post_model_outputs_response = stub.PostModelOutputs(
42
- service_pb2.PostModelOutputsRequest(
43
- user_app_id=userDataObject, # The userDataObject is created in the overview and is required when using a PAT
44
- model_id=GlobalConfig.CLARIFAI_MODEL_ID,
45
- # version_id=MODEL_VERSION_ID, # This is optional. Defaults to the latest model version
46
- inputs=[
47
- resources_pb2.Input(
48
- data=resources_pb2.Data(
49
- text=resources_pb2.Text(
50
- # url=TEXT_FILE_URL,
51
- raw=RAW_TEXT
 
 
52
  )
53
  )
54
- )
55
- ]
56
- ),
57
- metadata=metadata
58
- )
59
- if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
60
- print(post_model_outputs_response.status)
61
- raise Exception(f"Post model outputs failed, status: {post_model_outputs_response.status.description}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # Since we have one input, one output will exist here
64
- output = post_model_outputs_response.outputs[0]
 
 
65
 
66
- print("Completion:\n")
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)