File size: 2,503 Bytes
83eaee2
0b48655
83eaee2
 
 
 
968fadd
 
83eaee2
 
 
968fadd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83eaee2
 
 
 
 
 
 
968fadd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# !pip install gradio

import gradio as gr
from typing import List, Tuple
import numpy as np

import google.generativeai as genai

def reset() -> List:
    return []

def interact_summarization(prompt: str, article: str, temp = 1.0) -> List[Tuple[str, str]]:
    '''
      * Arguments
        - prompt: the prompt that we use in this section
        - article: the article to be summarized
        - temp: the temperature parameter of this model. Temperature is used to control the output of the chatbot. The higher the temperature is, the more creative response you will get.
    '''
    input = f"{prompt}\n{article}"
    response = model.generate_content(
      input,
      generation_config=genai.types.GenerationConfig(temperature=temp),
      safety_settings=[
          {"category": "HARM_CATEGORY_HARASSMENT","threshold": "BLOCK_NONE",},
          {"category": "HARM_CATEGORY_HATE_SPEECH","threshold": "BLOCK_NONE",},
          {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold": "BLOCK_NONE",},
          {"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE",},
          ]
    )

    return [(input, response.text)]

def interact(chatbot: List[Tuple[str, str]], user_input: str) -> List[Tuple[str, str]]:
    responses = ["You are right", "HaHa", "I don't know"]
    response = np.random.choice(responses, 1)[0]
    chatbot.append((user_input, response))

    return chatbot


if __name__ == '__main__':
    GOOGLE_API_KEY="AIzaSyB3pzEEQcMhqmJeIhfkOVebH7C4dm_4yTc"
    genai.configure(api_key=GOOGLE_API_KEY)
    model = genai.GenerativeModel('gemini-pro')

    with gr.Blocks() as demo:
        gr.Markdown(f"# Gradio Tutorial")
        chatbot = gr.Chatbot()
        prompt_textbox = gr.Textbox(label="Prompt", value='Please summarize the following article', visible=True)
        input_textbox = gr.Textbox(label="Input", value = "")
        with gr.Column():
            gr.Markdown("#  Temperature\n Temperature is used to control the output of the chatbot. The higher the temperature is, the more creative response you will get.")
            temperature_slider = gr.Slider(0.0, 1.0, 0.7, step = 0.1, label="Temperature")
        with gr.Row():
            sent_button = gr.Button(value="Send")
            reset_button = gr.Button(value="Reset")
        sent_button.click(interact_summarization, inputs=[prompt_textbox, input_textbox, temperature_slider], outputs=[chatbot])
        reset_button.click(reset, outputs=[chatbot])

    demo.launch(debug = True)