jerryjia92 commited on
Commit
968fadd
·
1 Parent(s): 83eaee2
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -4,9 +4,32 @@ import gradio as gr
4
  from typing import List, Tuple
5
  import numpy as np
6
 
 
 
7
  def reset() -> List:
8
  return []
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def interact(chatbot: List[Tuple[str, str]], user_input: str) -> List[Tuple[str, str]]:
11
  responses = ["You are right", "HaHa", "I don't know"]
12
  response = np.random.choice(responses, 1)[0]
@@ -14,14 +37,24 @@ def interact(chatbot: List[Tuple[str, str]], user_input: str) -> List[Tuple[str,
14
 
15
  return chatbot
16
 
17
- with gr.Blocks() as demo:
18
- gr.Markdown(f"# Gradio Tutorial")
19
- chatbot = gr.Chatbot()
20
- input_textbox = gr.Textbox(label="Input", value = "")
21
- with gr.Row():
22
- sent_button = gr.Button(value="Send")
23
- reset_button = gr.Button(value="Reset")
24
- sent_button.click(interact, inputs=[chatbot, input_textbox], outputs=[chatbot])
25
- reset_button.click(reset, outputs=[chatbot])
26
-
27
- demo.launch(debug = True)
 
 
 
 
 
 
 
 
 
 
 
4
  from typing import List, Tuple
5
  import numpy as np
6
 
7
+ import google.generativeai as genai
8
+
9
  def reset() -> List:
10
  return []
11
 
12
+ def interact_summarization(prompt: str, article: str, temp = 1.0) -> List[Tuple[str, str]]:
13
+ '''
14
+ * Arguments
15
+ - prompt: the prompt that we use in this section
16
+ - article: the article to be summarized
17
+ - 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.
18
+ '''
19
+ input = f"{prompt}\n{article}"
20
+ response = model.generate_content(
21
+ input,
22
+ generation_config=genai.types.GenerationConfig(temperature=temp),
23
+ safety_settings=[
24
+ {"category": "HARM_CATEGORY_HARASSMENT","threshold": "BLOCK_NONE",},
25
+ {"category": "HARM_CATEGORY_HATE_SPEECH","threshold": "BLOCK_NONE",},
26
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold": "BLOCK_NONE",},
27
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE",},
28
+ ]
29
+ )
30
+
31
+ return [(input, response.text)]
32
+
33
  def interact(chatbot: List[Tuple[str, str]], user_input: str) -> List[Tuple[str, str]]:
34
  responses = ["You are right", "HaHa", "I don't know"]
35
  response = np.random.choice(responses, 1)[0]
 
37
 
38
  return chatbot
39
 
40
+
41
+ if __name__ == '__main__':
42
+ GOOGLE_API_KEY="AIzaSyB3pzEEQcMhqmJeIhfkOVebH7C4dm_4yTc"
43
+ genai.configure(api_key=GOOGLE_API_KEY)
44
+ model = genai.GenerativeModel('gemini-pro')
45
+
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown(f"# Gradio Tutorial")
48
+ chatbot = gr.Chatbot()
49
+ prompt_textbox = gr.Textbox(label="Prompt", value='Please summarize the following article', visible=True)
50
+ input_textbox = gr.Textbox(label="Input", value = "")
51
+ with gr.Column():
52
+ 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.")
53
+ temperature_slider = gr.Slider(0.0, 1.0, 0.7, step = 0.1, label="Temperature")
54
+ with gr.Row():
55
+ sent_button = gr.Button(value="Send")
56
+ reset_button = gr.Button(value="Reset")
57
+ sent_button.click(interact_summarization, inputs=[prompt_textbox, input_textbox, temperature_slider], outputs=[chatbot])
58
+ reset_button.click(reset, outputs=[chatbot])
59
+
60
+ demo.launch(debug = True)