Khalil04 commited on
Commit
f67e67e
·
1 Parent(s): 8d9503a

Upload 4 files

Browse files
Files changed (4) hide show
  1. api.py +96 -0
  2. app.py +33 -0
  3. llama.py +74 -0
  4. requirements.txt +2 -0
api.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, Depends, HTTPException
2
+ from fastapi.responses import JSONResponse, RedirectResponse
3
+ from typing import Optional
4
+ from pydantic import BaseModel
5
+ ######################################################################################################
6
+ # In this section, we set the user authentication, user and app ID, model details, and the URL of
7
+ # the text we want as an input. Change these strings to run your own example.
8
+ ######################################################################################################
9
+
10
+ from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
11
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
12
+ from clarifai_grpc.grpc.api.status import status_code_pb2
13
+ import streamlit as st
14
+
15
+ app = FastAPI()
16
+
17
+ # Your PAT (Personal Access Token) can be found in the portal under Authentification
18
+ PAT = ''
19
+ # Specify the correct user_id/app_id pairings
20
+ # Since you're making inferences outside your app's scope
21
+ USER_ID = 'aianytime'
22
+ APP_ID = 'Llama2-Chat-App'
23
+ # Change these to whatever model and text URL you want to use
24
+ WORKFLOW_ID = 'Llama-2-Workflow'
25
+ TEXT_FILE_URL = 'https://samples.clarifai.com/negative_sentence_12.txt'
26
+
27
+ ############################################################################
28
+ # YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
29
+ ############################################################################
30
+
31
+ class InputPrompt(BaseModel):
32
+ prompt:str
33
+
34
+ def get_response(prompt):
35
+ channel = ClarifaiChannel.get_grpc_channel()
36
+ stub = service_pb2_grpc.V2Stub(channel)
37
+
38
+ metadata = (('authorization', 'Key ' + PAT),)
39
+
40
+ userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
41
+
42
+ response = "" # save response from the model
43
+
44
+ post_workflow_results_response = stub.PostWorkflowResults(
45
+ service_pb2.PostWorkflowResultsRequest(
46
+ user_app_id=userDataObject,
47
+ workflow_id=WORKFLOW_ID,
48
+ inputs=[
49
+ resources_pb2.Input(
50
+ data=resources_pb2.Data(
51
+ text=resources_pb2.Text(
52
+ raw=prompt
53
+ )
54
+ )
55
+ )
56
+ ]
57
+ ),
58
+ metadata=metadata
59
+ )
60
+
61
+ if post_workflow_results_response.status.code != status_code_pb2.SUCCESS:
62
+ print(post_workflow_results_response.status)
63
+
64
+ return response
65
+
66
+ # We'll get one WorkflowResult for each input we used above. Because of one input, we have here one WorkflowResult
67
+ results = post_workflow_results_response.results[0]
68
+
69
+ # Each model we have in the workflow will produce one output.
70
+ for output in results.outputs:
71
+ model = output.model
72
+
73
+ print("Predicted concepts for the model `%s`" % model.id)
74
+ for concept in output.data.concepts:
75
+ print(" %s %.2f" % (concept.name, concept.value))
76
+
77
+ response += output.data.text.raw + "\n"
78
+
79
+ # Uncomment this line to print the full Response JSON
80
+ # print(results)
81
+ print(response)
82
+
83
+ return response
84
+
85
+ @app.get("/get_model_response/", response_model=dict)
86
+ async def get_model_response(prompt: str = Query(..., title="Input Prompt", description="User Input prompt to the model")):
87
+ response = get_response(prompt)
88
+ return JSONResponse(content={"Model Response": response})
89
+
90
+ @app.get("/", include_in_schema=False)
91
+ async def redirect_to_docs():
92
+ return RedirectResponse(url="/docs")
93
+
94
+ if __name__ == "__main__":
95
+ import uvicorn
96
+ uvicorn.run(app, host="0.0.0.0", port=8000)
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import llama
3
+
4
+ message_data = []
5
+
6
+
7
+ def main():
8
+
9
+ st.title("Llama 2 Chat App")
10
+
11
+ # Initialize chat history
12
+ if 'chat_history' not in st.session_state:
13
+ st.session_state.chat_history = ""
14
+
15
+ st.info("Ask Your Query Below")
16
+
17
+ input_text = st.text_area("Enter your query")
18
+
19
+ if input_text is not None:
20
+ if st.button("Chat"):
21
+ st.info("Your Query: " + input_text)
22
+ st.session_state.chat_history += "User: " +" "+ str(input_text) + "\n"
23
+ result = llama.get_response(input_text)
24
+ st.session_state.chat_history += "AI Assistant: " + " "+str(result) + "\n"
25
+ st.success("Response:" + str(result))
26
+
27
+ st.text_area("Chat History:", value="\n"+st.session_state.chat_history +"\n", height=300)
28
+ session_data = "\n"+st.session_state.chat_history+"\n"
29
+ message_data.append(session_data)
30
+
31
+ # Run the Streamlit application
32
+ if __name__ == "__main__":
33
+ main()
llama.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ######################################################################################################
2
+ # In this section, we set the user authentication, user and app ID, model details, and the URL of
3
+ # the text we want as an input. Change these strings to run your own example.
4
+ ######################################################################################################
5
+
6
+ from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
7
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
8
+ from clarifai_grpc.grpc.api.status import status_code_pb2
9
+ import streamlit as st
10
+
11
+ # Your PAT (Personal Access Token) can be found in the portal under Authentification
12
+ PAT = st.secrets.PAT
13
+ # Specify the correct user_id/app_id pairings
14
+ # Since you're making inferences outside your app's scope
15
+ USER_ID = st.secrets.USER_ID
16
+ APP_ID = st.secrets.APP_ID
17
+ # Change these to whatever model and text URL you want to use
18
+ WORKFLOW_ID = 'Llama-2-Workflow'
19
+ TEXT_FILE_URL = 'https://samples.clarifai.com/negative_sentence_12.txt'
20
+
21
+ ############################################################################
22
+ # YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
23
+ ############################################################################
24
+
25
+ def get_response(prompt):
26
+ channel = ClarifaiChannel.get_grpc_channel()
27
+ stub = service_pb2_grpc.V2Stub(channel)
28
+
29
+ metadata = (('authorization', 'Key ' + PAT),)
30
+
31
+ userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
32
+
33
+ response = "" # save response from the model
34
+
35
+ post_workflow_results_response = stub.PostWorkflowResults(
36
+ service_pb2.PostWorkflowResultsRequest(
37
+ user_app_id=userDataObject,
38
+ workflow_id=WORKFLOW_ID,
39
+ inputs=[
40
+ resources_pb2.Input(
41
+ data=resources_pb2.Data(
42
+ text=resources_pb2.Text(
43
+ raw=prompt
44
+ )
45
+ )
46
+ )
47
+ ]
48
+ ),
49
+ metadata=metadata
50
+ )
51
+
52
+ if post_workflow_results_response.status.code != status_code_pb2.SUCCESS:
53
+ print(post_workflow_results_response.status)
54
+
55
+ return response
56
+
57
+ # We'll get one WorkflowResult for each input we used above. Because of one input, we have here one WorkflowResult
58
+ results = post_workflow_results_response.results[0]
59
+
60
+ # Each model we have in the workflow will produce one output.
61
+ for output in results.outputs:
62
+ model = output.model
63
+
64
+ print("Predicted concepts for the model `%s`" % model.id)
65
+ for concept in output.data.concepts:
66
+ print(" %s %.2f" % (concept.name, concept.value))
67
+
68
+ response += output.data.text.raw + "\n"
69
+
70
+ # Uncomment this line to print the full Response JSON
71
+ # print(results)
72
+ print(response)
73
+
74
+ return response
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ clarifai-grpc