rmdhirr commited on
Commit
99ca6b3
1 Parent(s): 12ef56f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ author: Elena Lowery
3
+
4
+ This code sample shows how to invoke Large Language Models (LLMs) deployed in watsonx.ai.
5
+ Documentation: https://ibm.github.io/watson-machine-learning-sdk/foundation_models.html
6
+ You will need to provide your IBM Cloud API key and a watonx.ai project id (any project)
7
+ for accessing watsonx.ai in a .env file
8
+ This example shows simple use cases without comprehensive prompt tuning
9
+ """
10
+
11
+ # Install the wml api in your Python environment prior to running this example:
12
+ # pip install ibm-watson-machine-learning
13
+ # pip install ibm-cloud-sdk-core
14
+ # pip install python-dotenv
15
+ # pip install gradio
16
+
17
+ # For reading credentials from the .env file
18
+ import os
19
+ from dotenv import load_dotenv
20
+
21
+ # WML python SDK
22
+ from ibm_watson_machine_learning.foundation_models import Model
23
+ from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
24
+ from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes, DecodingMethods
25
+
26
+ # For invocation of LLM with REST API
27
+ import requests, json
28
+ from ibm_cloud_sdk_core import IAMTokenManager
29
+
30
+ # For creating Gradio interface
31
+ import gradio as gr
32
+
33
+ # URL of the hosted LLMs is hardcoded because at this time all LLMs share the same endpoint
34
+ url = "https://us-south.ml.cloud.ibm.com"
35
+
36
+ # These global variables will be updated in get_credentials() functions
37
+ watsonx_project_id = ""
38
+ # Replace with your IBM Cloud key
39
+ api_key = ""
40
+
41
+ def get_credentials():
42
+ load_dotenv()
43
+ # Update the global variables that will be used for authentication in another function
44
+ globals()["api_key"] = os.getenv("api_key", None)
45
+ globals()["watsonx_project_id"] = os.getenv("project_id", None)
46
+
47
+ # The get_model function creates an LLM model object with the specified parameters
48
+ def get_model(model_type, max_tokens, min_tokens, decoding, temperature):
49
+ generate_params = {
50
+ GenParams.MAX_NEW_TOKENS: max_tokens,
51
+ GenParams.MIN_NEW_TOKENS: min_tokens,
52
+ GenParams.DECODING_METHOD: decoding,
53
+ GenParams.TEMPERATURE: temperature
54
+ }
55
+
56
+ model = Model(
57
+ model_id=model_type,
58
+ params=generate_params,
59
+ credentials={
60
+ "apikey": api_key,
61
+ "url": url
62
+ },
63
+ project_id=watsonx_project_id
64
+ )
65
+
66
+ return model
67
+
68
+ def generate_response(model_type, prompt, max_tokens, min_tokens, decoding, temperature):
69
+ model = get_model(model_type, max_tokens, min_tokens, decoding, temperature)
70
+ generated_response = model.generate(prompt=prompt)
71
+ return generated_response['results'][0]['generated_text']
72
+
73
+ def demo_LLM_invocation(prompt, model_type="google/flan-ul2", max_tokens=300, min_tokens=50, decoding="sample", temperature=0.7):
74
+ get_credentials()
75
+ response = generate_response(model_type, prompt, max_tokens, min_tokens, decoding, temperature)
76
+ return response
77
+
78
+ # Gradio interface
79
+ def gradio_interface(prompt):
80
+ response = demo_LLM_invocation(prompt)
81
+ return response
82
+
83
+ # Create a Gradio app
84
+ iface = gr.Interface(
85
+ fn=gradio_interface,
86
+ inputs="text",
87
+ outputs="text",
88
+ title="🌠 Test watsonx.ai LLM",
89
+ description="Ask a question and get a response from the IBM Watson LLM. For example: 'What is IBM?'"
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ iface.launch()