Files changed (1) hide show
  1. app.py +58 -67
app.py CHANGED
@@ -1,67 +1,58 @@
1
- import os
2
-
3
- import openai
4
- import gradio as gr
5
-
6
- openai.api_key = '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'
7
- #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
8
-
9
- def clean_textbox(*args):
10
- n = len(args)
11
- return [""] * n
12
-
13
-
14
- class ChatGPT:
15
- def __init__(self):
16
- self.messages = [{'role': 'system', 'content': "You are now a very useful maid assistant! If you have a question you can't answer, please reply with As a classy, I can't answer this question"}]
17
-
18
- def reset(self, *args):
19
- self.messages = [{'role': 'system', 'content': "You are now a very useful maid assistant! If you have a question you can't answer, please reply with As a classy, I can't answer this question"}]
20
- return clean_textbox(*args)
21
-
22
- def chat(self, prompt):
23
- self.messages.append({"role": "user", "content": prompt})
24
- completion = openai.ChatCompletion.create(
25
- model="gpt-3.5-turbo",
26
- messages=self.messages,
27
- temperature=0.7
28
- )
29
- res_msg = completion.choices[0].message["content"].strip()
30
- self.messages.append({"role": "assistant", "content": res_msg})
31
- return res_msg
32
-
33
-
34
- if __name__ == '__main__':
35
- my_chatgpt = ChatGPT()
36
- with gr.Blocks(title="POULTA INC ChatGPT") as demo:
37
- gr.Markdown('''
38
- # ChatGPT
39
- ''')
40
- with gr.Row():
41
- with gr.Column(scale=9):
42
- prompt = gr.Text(label='ChatGPT_Prompt', show_label=False, lines=3,
43
- placeholder='ChatGPT Prompt')
44
- res = gr.Text(label='ChatGPT_result', show_label=False, lines=3,
45
- placeholder='chatgpt results')
46
-
47
- with gr.Column(scale=1):
48
- btn_gen = gr.Button(value="send", variant='primary')
49
- btn_clear = gr.Button(value="restart chat")
50
-
51
- gr.Examples([
52
- ["What is Poulta Inc?"],
53
- ["what is Poulta inc doing?"],
54
- ["Poulta inc Bring real-time central monitoring to your farms"]],
55
- inputs=[prompt],
56
- outputs=[res],
57
- fn=my_chatgpt.chat,
58
- cache_examples=False)
59
-
60
- btn_gen.click(fn=my_chatgpt.chat, inputs=prompt,
61
- outputs=res)
62
- btn_clear.click(fn=my_chatgpt.reset,
63
- inputs=[prompt, res],
64
- outputs=[prompt, res])
65
-
66
- demo.queue()
67
- demo.launch()
 
1
+ import streamlit as st
2
+ from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
3
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
4
+ from clarifai_grpc.grpc.api.status import status_code_pb2
5
+
6
+ # Function to make API call and get text completion
7
+ def get_text_completion(raw_text):
8
+ PAT = '9b209aadda08410caf0b6d815b57e080'
9
+ USER_ID = 'openai'
10
+ APP_ID = 'chat-completion'
11
+ MODEL_ID = 'GPT-4'
12
+ MODEL_VERSION_ID = '5d7a50b44aec4a01a9c492c5a5fcf387'
13
+
14
+ channel = ClarifaiChannel.get_grpc_channel()
15
+ stub = service_pb2_grpc.V2Stub(channel)
16
+ metadata = (('authorization', 'Key ' + PAT),)
17
+
18
+ userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
19
+
20
+ post_model_outputs_response = stub.PostModelOutputs(
21
+ service_pb2.PostModelOutputsRequest(
22
+ user_app_id=userDataObject,
23
+ model_id=MODEL_ID,
24
+ version_id=MODEL_VERSION_ID,
25
+ inputs=[
26
+ resources_pb2.Input(
27
+ data=resources_pb2.Data(
28
+ text=resources_pb2.Text(
29
+ raw=raw_text
30
+ )
31
+ )
32
+ )
33
+ ]
34
+ ),
35
+ metadata=metadata
36
+ )
37
+
38
+ if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
39
+ raise Exception(f"Post model outputs failed, status: {post_model_outputs_response.status.description}")
40
+
41
+ output = post_model_outputs_response.outputs[0]
42
+
43
+ return output.data.text.raw
44
+
45
+ # Streamlit app
46
+ def main():
47
+ st.title("Text Completion App")
48
+
49
+ # Get user input
50
+ raw_text = st.text_area("Enter text:", "i need to know about narcotics pemishment")
51
+
52
+ # Perform text completion when button is clicked
53
+ if st.button("Complete Text"):
54
+ completion_result = get_text_completion(raw_text)
55
+ st.success("Completion:\n{}".format(completion_result))
56
+
57
+ if __name__ == "__main__":
58
+ main()