text_summary / app.py
jerryjia92's picture
changed
968fadd
# !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)