Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,12 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import google.generativeai as palm
|
|
|
|
| 5 |
|
| 6 |
llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
|
| 7 |
TEST_MESSAGE = "Write an introductory paragraph to explain Generative AI to the reader of this content."
|
| 8 |
openai_models = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo",
|
| 9 |
-
"gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "text-davinci-003",
|
| 10 |
"text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001"]
|
| 11 |
|
| 12 |
google_palm_models = ["models/text-bison-001", "models/chat-bison-001","models/embedding-gecko-001"]
|
|
@@ -25,10 +26,10 @@ def openai_text_completion(openai_api_key: str, prompt: str, model: str):
|
|
| 25 |
openai.api_key = openai_api_key
|
| 26 |
openai.api_version = '2020-11-07'
|
| 27 |
completion = openai.ChatCompletion.create(
|
| 28 |
-
model = model,
|
| 29 |
messages = messages,
|
| 30 |
temperature = temperature
|
| 31 |
-
)
|
| 32 |
response = completion["choices"][0]["message"].content
|
| 33 |
return "", response
|
| 34 |
except Exception as exception:
|
|
@@ -47,14 +48,14 @@ def azure_openai_text_completion(azure_openai_api_key: str, azure_endpoint: str,
|
|
| 47 |
]
|
| 48 |
openai.api_key = azure_openai_api_key
|
| 49 |
openai.api_type = "azure"
|
| 50 |
-
openai.api_version = "2023-05-15"
|
| 51 |
openai.api_base = f"https://{azure_endpoint}.openai.azure.com"
|
| 52 |
completion = openai.ChatCompletion.create(
|
| 53 |
-
model = model,
|
| 54 |
engine = azure_deployment_name,
|
| 55 |
messages = messages,
|
| 56 |
temperature = temperature
|
| 57 |
-
)
|
| 58 |
response = completion["choices"][0]["message"].content
|
| 59 |
return "", response
|
| 60 |
except Exception as exception:
|
|
@@ -64,7 +65,7 @@ def azure_openai_text_completion(azure_openai_api_key: str, azure_endpoint: str,
|
|
| 64 |
|
| 65 |
|
| 66 |
def palm_text_completion(google_palm_key: str, prompt: str, model: str):
|
| 67 |
-
try:
|
| 68 |
candidate_count = 1
|
| 69 |
top_k = 40
|
| 70 |
top_p = 0.95
|
|
@@ -80,7 +81,6 @@ def palm_text_completion(google_palm_key: str, prompt: str, model: str):
|
|
| 80 |
'stop_sequences': [],
|
| 81 |
'safety_settings': [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}],
|
| 82 |
}
|
| 83 |
-
|
| 84 |
response = palm.generate_text(
|
| 85 |
**defaults,
|
| 86 |
prompt=prompt
|
|
@@ -91,15 +91,18 @@ def palm_text_completion(google_palm_key: str, prompt: str, model: str):
|
|
| 91 |
print(exception)
|
| 92 |
return f" palm_text_completion Error - {exception}", ""
|
| 93 |
|
| 94 |
-
def test_handler(optionSelection,
|
| 95 |
-
openai_key,
|
| 96 |
-
azure_openai_key,
|
| 97 |
-
azure_openai_api_base,
|
| 98 |
-
azure_openai_deployment_name,
|
| 99 |
-
google_generative_api_key,
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
match optionSelection:
|
| 104 |
case "OpenAI API":
|
| 105 |
message, response = openai_text_completion(openai_key, prompt,openai_model_name)
|
|
@@ -111,18 +114,21 @@ def test_handler(optionSelection,
|
|
| 111 |
message, response = palm_text_completion(google_generative_api_key, prompt,google_model_name)
|
| 112 |
return message, response
|
| 113 |
case "Llama 2":
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
case _:
|
| 116 |
if optionSelection not in llm_api_options:
|
| 117 |
return ValueError("Invalid choice!"), ""
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
with gr.Blocks() as LLMDemoTabbedScreen:
|
| 122 |
with gr.Tab("Text-to-Text (Text Completion)"):
|
| 123 |
llm_options = gr.Radio(llm_api_options, label="Select one", info="Which service do you want to use?", value="OpenAI API")
|
| 124 |
with gr.Row():
|
| 125 |
-
with gr.Column():
|
| 126 |
test_string = gr.Textbox(label="Try String", value=TEST_MESSAGE, lines=5)
|
| 127 |
test_string_response = gr.Textbox(label="Response", lines=5)
|
| 128 |
test_string_output_info = gr.Label(value="Output Info", label="Info")
|
|
@@ -133,29 +139,36 @@ with gr.Blocks() as LLMDemoTabbedScreen:
|
|
| 133 |
openai_key = gr.Textbox(label="OpenAI API Key", type="password")
|
| 134 |
with gr.Tab("Azure Open AI"):
|
| 135 |
with gr.Row():
|
| 136 |
-
with gr.Column():
|
| 137 |
azure_openai_key = gr.Textbox(label="Azure OpenAI API Key", type="password")
|
| 138 |
azure_openai_api_base = gr.Textbox(label="Azure OpenAI API Endpoint")
|
| 139 |
azure_openai_deployment_name = gr.Textbox(label="Azure OpenAI API Deployment Name")
|
| 140 |
with gr.Tab("Google PaLM API"):
|
| 141 |
with gr.Row():
|
| 142 |
with gr.Column():
|
| 143 |
-
google_model_name = gr.Dropdown(google_palm_models, value="models/text-bison-001", label="Model", info="Select one, for Natural language")
|
| 144 |
google_generative_api_key = gr.Textbox(label="Google Generative AI API Key", type="password")
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
test_button.click(
|
| 147 |
fn=test_handler,
|
| 148 |
-
inputs=[llm_options,
|
| 149 |
-
openai_key,
|
| 150 |
-
azure_openai_key,
|
| 151 |
-
azure_openai_api_base,
|
| 152 |
-
azure_openai_deployment_name,
|
| 153 |
-
google_generative_api_key,
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
outputs=[test_string_output_info, test_string_response]
|
| 158 |
)
|
| 159 |
-
|
| 160 |
if __name__ == "__main__":
|
| 161 |
LLMDemoTabbedScreen.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import google.generativeai as palm
|
| 5 |
+
import together
|
| 6 |
|
| 7 |
llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
|
| 8 |
TEST_MESSAGE = "Write an introductory paragraph to explain Generative AI to the reader of this content."
|
| 9 |
openai_models = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo",
|
| 10 |
+
"gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "text-davinci-003",
|
| 11 |
"text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001"]
|
| 12 |
|
| 13 |
google_palm_models = ["models/text-bison-001", "models/chat-bison-001","models/embedding-gecko-001"]
|
|
|
|
| 26 |
openai.api_key = openai_api_key
|
| 27 |
openai.api_version = '2020-11-07'
|
| 28 |
completion = openai.ChatCompletion.create(
|
| 29 |
+
model = model,
|
| 30 |
messages = messages,
|
| 31 |
temperature = temperature
|
| 32 |
+
)
|
| 33 |
response = completion["choices"][0]["message"].content
|
| 34 |
return "", response
|
| 35 |
except Exception as exception:
|
|
|
|
| 48 |
]
|
| 49 |
openai.api_key = azure_openai_api_key
|
| 50 |
openai.api_type = "azure"
|
| 51 |
+
openai.api_version = "2023-05-15"
|
| 52 |
openai.api_base = f"https://{azure_endpoint}.openai.azure.com"
|
| 53 |
completion = openai.ChatCompletion.create(
|
| 54 |
+
model = model,
|
| 55 |
engine = azure_deployment_name,
|
| 56 |
messages = messages,
|
| 57 |
temperature = temperature
|
| 58 |
+
)
|
| 59 |
response = completion["choices"][0]["message"].content
|
| 60 |
return "", response
|
| 61 |
except Exception as exception:
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
def palm_text_completion(google_palm_key: str, prompt: str, model: str):
|
| 68 |
+
try:
|
| 69 |
candidate_count = 1
|
| 70 |
top_k = 40
|
| 71 |
top_p = 0.95
|
|
|
|
| 81 |
'stop_sequences': [],
|
| 82 |
'safety_settings': [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}],
|
| 83 |
}
|
|
|
|
| 84 |
response = palm.generate_text(
|
| 85 |
**defaults,
|
| 86 |
prompt=prompt
|
|
|
|
| 91 |
print(exception)
|
| 92 |
return f" palm_text_completion Error - {exception}", ""
|
| 93 |
|
| 94 |
+
def test_handler(optionSelection,
|
| 95 |
+
openai_key,
|
| 96 |
+
azure_openai_key,
|
| 97 |
+
azure_openai_api_base,
|
| 98 |
+
azure_openai_deployment_name,
|
| 99 |
+
google_generative_api_key,
|
| 100 |
+
together_api_key,
|
| 101 |
+
prompt: str = TEST_MESSAGE,
|
| 102 |
+
openai_model_name: str ="gpt-4",
|
| 103 |
+
google_model_name: str ="models/text-bison-001",
|
| 104 |
+
together_model_name: str = "togethercomputer/llama-2-70b-chat"
|
| 105 |
+
):
|
| 106 |
match optionSelection:
|
| 107 |
case "OpenAI API":
|
| 108 |
message, response = openai_text_completion(openai_key, prompt,openai_model_name)
|
|
|
|
| 114 |
message, response = palm_text_completion(google_generative_api_key, prompt,google_model_name)
|
| 115 |
return message, response
|
| 116 |
case "Llama 2":
|
| 117 |
+
together.api_key = together_api_key
|
| 118 |
+
model: str = together_model_name
|
| 119 |
+
output = together.Complete.create(prompt, model=model,temperature=temperature)
|
| 120 |
+
return "Response from Together API", output['output']['choices'][0]['text']
|
| 121 |
case _:
|
| 122 |
if optionSelection not in llm_api_options:
|
| 123 |
return ValueError("Invalid choice!"), ""
|
| 124 |
|
| 125 |
+
|
| 126 |
|
| 127 |
with gr.Blocks() as LLMDemoTabbedScreen:
|
| 128 |
with gr.Tab("Text-to-Text (Text Completion)"):
|
| 129 |
llm_options = gr.Radio(llm_api_options, label="Select one", info="Which service do you want to use?", value="OpenAI API")
|
| 130 |
with gr.Row():
|
| 131 |
+
with gr.Column():
|
| 132 |
test_string = gr.Textbox(label="Try String", value=TEST_MESSAGE, lines=5)
|
| 133 |
test_string_response = gr.Textbox(label="Response", lines=5)
|
| 134 |
test_string_output_info = gr.Label(value="Output Info", label="Info")
|
|
|
|
| 139 |
openai_key = gr.Textbox(label="OpenAI API Key", type="password")
|
| 140 |
with gr.Tab("Azure Open AI"):
|
| 141 |
with gr.Row():
|
| 142 |
+
with gr.Column():
|
| 143 |
azure_openai_key = gr.Textbox(label="Azure OpenAI API Key", type="password")
|
| 144 |
azure_openai_api_base = gr.Textbox(label="Azure OpenAI API Endpoint")
|
| 145 |
azure_openai_deployment_name = gr.Textbox(label="Azure OpenAI API Deployment Name")
|
| 146 |
with gr.Tab("Google PaLM API"):
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column():
|
| 149 |
+
google_model_name = gr.Dropdown(google_palm_models, value="models/text-bison-001", label="Model", info="Select one, for Natural language")
|
| 150 |
google_generative_api_key = gr.Textbox(label="Google Generative AI API Key", type="password")
|
| 151 |
+
with gr.Tab("Llama-2"):
|
| 152 |
+
with gr.Row():
|
| 153 |
+
with gr.Column():
|
| 154 |
+
together_model_name = gr.Dropdown(['togethercomputer/llama-2-70b-chat'], value="togethercomputer/llama-2-70b-chat", label="Model", info="Select one, for Natural language")
|
| 155 |
+
together_api_key = gr.Textbox(label="Together API Key", type="password")
|
| 156 |
+
|
| 157 |
test_button.click(
|
| 158 |
fn=test_handler,
|
| 159 |
+
inputs=[llm_options,
|
| 160 |
+
openai_key,
|
| 161 |
+
azure_openai_key,
|
| 162 |
+
azure_openai_api_base,
|
| 163 |
+
azure_openai_deployment_name,
|
| 164 |
+
google_generative_api_key,
|
| 165 |
+
together_api_key,
|
| 166 |
+
test_string,
|
| 167 |
+
openai_model,
|
| 168 |
+
google_model_name,
|
| 169 |
+
together_model_name],
|
| 170 |
outputs=[test_string_output_info, test_string_response]
|
| 171 |
)
|
| 172 |
+
|
| 173 |
if __name__ == "__main__":
|
| 174 |
LLMDemoTabbedScreen.launch()
|