Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -8,31 +8,36 @@ import openai | |
| 8 |  | 
| 9 | 
             
            llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
         | 
| 10 | 
             
            TEST_MESSAGE = "My favorite TV shows are The Mentalist, The Blacklist, Designated Survivor, and Unforgettable. What are ten series that I should watch next?"
         | 
|  | |
|  | |
|  | |
| 11 |  | 
| 12 | 
             
            def test_handler(optionSelection, prompt: str = "Write an introductory paragraph to explain Generative AI to the reader of this content."):        
         | 
| 13 | 
             
                match optionSelection:
         | 
| 14 | 
             
                    case  "OpenAI API":
         | 
| 15 | 
            -
                         | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
                             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
                             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
|  | |
|  | |
| 36 | 
             
                    case  "Azure OpenAI API":
         | 
| 37 | 
             
                        return "", ""
         | 
| 38 | 
             
                    case  "Google PaLM API":
         | 
| @@ -48,10 +53,15 @@ def test_handler(optionSelection, prompt: str = "Write an introductory paragraph | |
| 48 | 
             
            with gr.Blocks() as LLMDemoTabbedScreen:
         | 
| 49 | 
             
                with gr.Tab("Text-to-Text (Text Completion)"):
         | 
| 50 | 
             
                    llm_options = gr.Radio(llm_api_options, label="Select one", info="Which service do you want to use?", value="OpenAI API")
         | 
| 51 | 
            -
                     | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 55 |  | 
| 56 |  | 
| 57 | 
             
                test_button.click(
         | 
|  | |
| 8 |  | 
| 9 | 
             
            llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
         | 
| 10 | 
             
            TEST_MESSAGE = "My favorite TV shows are The Mentalist, The Blacklist, Designated Survivor, and Unforgettable. What are ten series that I should watch next?"
         | 
| 11 | 
            +
            openai_models = ["gpt-4", "gpt-4-0613", "gpt-4-32k", "gpt-4-32k-0613", "gpt-3.5-turbo", 
         | 
| 12 | 
            +
                                 "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "text-davinci-003", 
         | 
| 13 | 
            +
                                 "text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001"]
         | 
| 14 |  | 
| 15 | 
             
            def test_handler(optionSelection, prompt: str = "Write an introductory paragraph to explain Generative AI to the reader of this content."):        
         | 
| 16 | 
             
                match optionSelection:
         | 
| 17 | 
             
                    case  "OpenAI API":
         | 
| 18 | 
            +
                        try:
         | 
| 19 | 
            +
                            #model = "gpt-35-turbo"
         | 
| 20 | 
            +
                            model = "gpt-4"
         | 
| 21 | 
            +
                            system_prompt: str = "Explain in detail to help student understand the concept.", 
         | 
| 22 | 
            +
                            assistant_prompt: str = None, 
         | 
| 23 | 
            +
                            
         | 
| 24 | 
            +
                            messages = [
         | 
| 25 | 
            +
                                {"role": "user", "content": f"{prompt}"},
         | 
| 26 | 
            +
                                {"role": "system", "content": f"{system_prompt}"},
         | 
| 27 | 
            +
                                {"role": "assistant", "content": f"{assistant_prompt}"}
         | 
| 28 | 
            +
                            ]
         | 
| 29 | 
            +
                            
         | 
| 30 | 
            +
                            openai.api_key = os.getenv("OPENAI_API_KEY")
         | 
| 31 | 
            +
                            openai.api_version = '2020-11-07'
         | 
| 32 | 
            +
                            
         | 
| 33 | 
            +
                            completion = openai.ChatCompletion.create(
         | 
| 34 | 
            +
                                model = model, 
         | 
| 35 | 
            +
                                messages = messages,
         | 
| 36 | 
            +
                                temperature = 0.7
         | 
| 37 | 
            +
                            )           
         | 
| 38 | 
            +
                            response = completion["choices"][0]["message"].content
         | 
| 39 | 
            +
                            return "", response
         | 
| 40 | 
            +
                        except openai.error.ServiceUnavailableError:
         | 
| 41 | 
             
                    case  "Azure OpenAI API":
         | 
| 42 | 
             
                        return "", ""
         | 
| 43 | 
             
                    case  "Google PaLM API":
         | 
|  | |
| 53 | 
             
            with gr.Blocks() as LLMDemoTabbedScreen:
         | 
| 54 | 
             
                with gr.Tab("Text-to-Text (Text Completion)"):
         | 
| 55 | 
             
                    llm_options = gr.Radio(llm_api_options, label="Select one", info="Which service do you want to use?", value="OpenAI API")
         | 
| 56 | 
            +
                    with gr.Tab("Open AI"):
         | 
| 57 | 
            +
                        openai_model = gr.Dropdown(openai_models, value="gpt-4", label="Model", info="Select one, for Natural language")
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                    with gr.Row():
         | 
| 60 | 
            +
                        with gr.Column(): 
         | 
| 61 | 
            +
                            test_string = gr.Textbox(label="Try String", value=TEST_MESSAGE, lines=2)
         | 
| 62 | 
            +
                            test_string_response = gr.Textbox(label="Response")
         | 
| 63 | 
            +
                            test_string_output_info = gr.Label(value="Output Info", label="Info")
         | 
| 64 | 
            +
                            test_button = gr.Button("Try it")
         | 
| 65 |  | 
| 66 |  | 
| 67 | 
             
                test_button.click(
         | 
