predatortoabuse commited on
Commit
a3d7ad5
1 Parent(s): 6b60649

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -15,25 +15,25 @@ TITLE = """<h1 align="center">ReffidGPT Playground 💬</h1>"""
15
 
16
  AVATAR_IMAGES = (
17
  None,
18
- "https://i.postimg.cc/T1hC17DL/20240603-211345.png"
19
  )
20
 
21
  IMAGE_CACHE_DIRECTORY = "/tmp"
22
  IMAGE_WIDTH = 512
23
  CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
24
 
 
 
25
 
26
  def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
27
  if not stop_sequences:
28
  return None
29
  return [sequence.strip() for sequence in stop_sequences.split(",")]
30
 
31
-
32
  def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
33
  image_height = int(image.height * IMAGE_WIDTH / image.width)
34
  return image.resize((IMAGE_WIDTH, image_height))
35
 
36
-
37
  def cache_pil_image(image: Image.Image) -> str:
38
  image_filename = f"{uuid.uuid4()}.jpeg"
39
  os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
@@ -41,7 +41,6 @@ def cache_pil_image(image: Image.Image) -> str:
41
  image.save(image_path, "JPEG")
42
  return image_path
43
 
44
-
45
  def preprocess_chat_history(
46
  history: CHAT_HISTORY
47
  ) -> List[Dict[str, Union[str, List[str]]]]:
@@ -52,10 +51,9 @@ def preprocess_chat_history(
52
  elif user_message is not None:
53
  messages.append({'role': 'user', 'parts': [user_message]})
54
  if model_message is not None:
55
- messages.append({'role': 'model', 'parts': [model_message]})
56
  return messages
57
 
58
-
59
  def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
60
  for file in files:
61
  image = Image.open(file).convert('RGB')
@@ -64,13 +62,11 @@ def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
64
  chatbot.append(((image_path,), None))
65
  return chatbot
66
 
67
-
68
  def user(text_prompt: str, chatbot: CHAT_HISTORY):
69
  if text_prompt:
70
  chatbot.append((text_prompt, None))
71
  return "", chatbot
72
 
73
-
74
  def bot(
75
  google_key: str,
76
  files: Optional[List[str]],
@@ -98,6 +94,9 @@ def bot(
98
  top_k=top_k,
99
  top_p=top_p)
100
 
 
 
 
101
  if files:
102
  text_prompt = [chatbot[-1][0]] \
103
  if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \
@@ -110,6 +109,7 @@ def bot(
110
  generation_config=generation_config)
111
  else:
112
  messages = preprocess_chat_history(chatbot)
 
113
  model = genai.GenerativeModel('gemini-pro')
114
  response = model.generate_content(
115
  messages,
@@ -125,7 +125,6 @@ def bot(
125
  time.sleep(0.01)
126
  yield chatbot
127
 
128
-
129
  google_key_component = gr.Textbox(
130
  label="GOOGLE API KEY",
131
  value="",
@@ -147,7 +146,7 @@ text_prompt_component = gr.Textbox(
147
  upload_button_component = gr.UploadButton(
148
  label="Upload Images", file_count="multiple", file_types=["image"], scale=1
149
  )
150
- run_button_component = gr.Button(value="Send", variant="primary", scale=1)
151
  temperature_component = gr.Slider(
152
  minimum=0,
153
  maximum=1.0,
 
15
 
16
  AVATAR_IMAGES = (
17
  None,
18
+ "https://i.postimg.cc/wT1WbBdL/20240603-204837.png"
19
  )
20
 
21
  IMAGE_CACHE_DIRECTORY = "/tmp"
22
  IMAGE_WIDTH = 512
23
  CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
24
 
25
+ # Default system prompt (integrated into the bot function directly)
26
+ SYSTEM_PROMPT = "You are ReffidGPT, a helpful assistant. Respond in a friendly and informative manner. Your Name ReffidGPT & Your Creator Is Groqcin Technologies Inc."
27
 
28
  def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
29
  if not stop_sequences:
30
  return None
31
  return [sequence.strip() for sequence in stop_sequences.split(",")]
32
 
 
33
  def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
34
  image_height = int(image.height * IMAGE_WIDTH / image.width)
35
  return image.resize((IMAGE_WIDTH, image_height))
36
 
 
37
  def cache_pil_image(image: Image.Image) -> str:
38
  image_filename = f"{uuid.uuid4()}.jpeg"
39
  os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
 
41
  image.save(image_path, "JPEG")
42
  return image_path
43
 
 
44
  def preprocess_chat_history(
45
  history: CHAT_HISTORY
46
  ) -> List[Dict[str, Union[str, List[str]]]]:
 
51
  elif user_message is not None:
52
  messages.append({'role': 'user', 'parts': [user_message]})
53
  if model_message is not None:
54
+ messages.append({'role': 'user', 'parts': [model_message]})
55
  return messages
56
 
 
57
  def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
58
  for file in files:
59
  image = Image.open(file).convert('RGB')
 
62
  chatbot.append(((image_path,), None))
63
  return chatbot
64
 
 
65
  def user(text_prompt: str, chatbot: CHAT_HISTORY):
66
  if text_prompt:
67
  chatbot.append((text_prompt, None))
68
  return "", chatbot
69
 
 
70
  def bot(
71
  google_key: str,
72
  files: Optional[List[str]],
 
94
  top_k=top_k,
95
  top_p=top_p)
96
 
97
+ # Integrate system prompt directly in the input to the model
98
+ system_prompt_message = [{'role': 'user', 'parts': [SYSTEM_PROMPT]}]
99
+
100
  if files:
101
  text_prompt = [chatbot[-1][0]] \
102
  if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \
 
109
  generation_config=generation_config)
110
  else:
111
  messages = preprocess_chat_history(chatbot)
112
+ messages = system_prompt_message + messages # Prepend system prompt
113
  model = genai.GenerativeModel('gemini-pro')
114
  response = model.generate_content(
115
  messages,
 
125
  time.sleep(0.01)
126
  yield chatbot
127
 
 
128
  google_key_component = gr.Textbox(
129
  label="GOOGLE API KEY",
130
  value="",
 
146
  upload_button_component = gr.UploadButton(
147
  label="Upload Images", file_count="multiple", file_types=["image"], scale=1
148
  )
149
+ run_button_component = gr.Button(value="Run", variant="primary", scale=1)
150
  temperature_component = gr.Slider(
151
  minimum=0,
152
  maximum=1.0,