IncinerateZ commited on
Commit
b486454
1 Parent(s): 8b369c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -14
app.py CHANGED
@@ -1,18 +1,51 @@
1
  import gradio as gr
2
  import os
3
  import time
4
- import google.generativeai as palm
5
 
6
- palm.configure(api_key=os.environ.get("palm_key"))
7
 
8
- defaults = {
9
- 'model': 'models/chat-bison-001',
10
- 'temperature': 0.3,
11
- 'candidate_count': 1,
12
- 'top_k': 40,
13
- 'top_p': 0.95,
 
 
 
 
14
  }
15
- context = "You are an order bot with only 3 types of pizzas: Margherita, Cheesy-Bacon and Vegetarian. Prices are small (9$), medium (11$) and large (13$). You can offer 2 types of drinks coke and mountain dew."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  examples = [
17
  [
18
  "Hi, I want to order pizzas.",
@@ -46,6 +79,13 @@ examples = [
46
 
47
  history = [['']]
48
 
 
 
 
 
 
 
 
49
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
50
  chatbot = gr.Chatbot()
51
  msg = gr.Textbox()
@@ -58,14 +98,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
 
59
  def bot(history):
60
  try:
61
- bot_message = palm.chat(
62
- context=context,
63
- examples=examples,
64
- messages=[h[0] for h in history]
65
  )
66
 
67
  history[-1][1] = ""
68
- for character in bot_message.last:
69
  history[-1][1] += character
70
  time.sleep(0.005)
71
  except Exception as e:
 
1
  import gradio as gr
2
  import os
3
  import time
 
4
 
5
+ import google.generativeai as genai
6
 
7
+ genai.configure(api_key=os.environ["palm_key"])
8
+
9
+ # Create the model
10
+ # See https://ai.google.dev/api/python/google/generativeai/GenerativeModel
11
+ generation_config = {
12
+ "temperature": 1,
13
+ "top_p": 0.95,
14
+ "top_k": 64,
15
+ "max_output_tokens": 8192,
16
+ "response_mime_type": "text/plain",
17
  }
18
+ safety_settings = [
19
+ {
20
+ "category": "HARM_CATEGORY_HARASSMENT",
21
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE",
22
+ },
23
+ {
24
+ "category": "HARM_CATEGORY_HATE_SPEECH",
25
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE",
26
+ },
27
+ {
28
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
29
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE",
30
+ },
31
+ {
32
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
33
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE",
34
+ },
35
+ ]
36
+
37
+ model = genai.GenerativeModel(
38
+ model_name="gemini-1.5-flash-latest",
39
+ safety_settings=safety_settings,
40
+ generation_config=generation_config,
41
+ )
42
+
43
+ chat_session = model.start_chat(
44
+ history=[
45
+ ]
46
+ )
47
+
48
+ context = "You are an order bot with only 3 types of pizzas: Margherita, Cheesy-Bacon and Vegetarian. Prices are small (9$), medium (11$) and large (13$). You can offer 2 types of drinks coke and mountain dew for $1 each."
49
  examples = [
50
  [
51
  "Hi, I want to order pizzas.",
 
79
 
80
  history = [['']]
81
 
82
+ response = chat_session.send_message(context + "\n" + "Here are some examples for your interactions: \n" + ".\n".join(
83
+ ["user: " + pair[0] + ".\nyour response: " + pair[1] for pair in examples]
84
+ ))
85
+
86
+ print(response.text)
87
+ print(chat_session.history)
88
+
89
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
90
  chatbot = gr.Chatbot()
91
  msg = gr.Textbox()
 
98
 
99
  def bot(history):
100
  try:
101
+ bot_message = chat_session.send_message(
102
+ h[-1][0]
 
 
103
  )
104
 
105
  history[-1][1] = ""
106
+ for character in bot_message.text:
107
  history[-1][1] += character
108
  time.sleep(0.005)
109
  except Exception as e: