hamada056 commited on
Commit
bd5a001
·
verified ·
1 Parent(s): 7abb98d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -25,8 +25,10 @@ def preprocess_image(image: Image.Image) -> Image.Image:
25
  image_height = int(image.height * IMAGE_WIDTH / image.width)
26
  return image.resize((IMAGE_WIDTH, image_height))
27
 
28
- def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
29
- return "", chatbot + [[text_prompt, None]]
 
 
30
 
31
  def bot(
32
  google_key: str,
@@ -36,24 +38,22 @@ def bot(
36
  stop_sequences: str,
37
  top_k: int,
38
  top_p: float,
39
- chatbot: List[Tuple[str, str]]
40
  ):
41
  google_key = google_key or GOOGLE_API_KEY
42
  if not google_key:
43
  raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
44
 
45
- text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
46
 
47
  # Handle cases for text and/or image input
48
  if not text_prompt and not image_prompt:
49
- chatbot[-1][1] = "Prompt cannot be empty. Please provide input text or an image."
50
  yield chatbot
51
  return
52
  elif image_prompt and not text_prompt:
53
- # If only an image is provided
54
  text_prompt = "Describe the image"
55
  elif image_prompt and text_prompt:
56
- # If both text and image are provided, combine them
57
  text_prompt = f"{text_prompt}. Also, analyze the provided image."
58
 
59
  # Configure the model
@@ -66,23 +66,20 @@ def bot(
66
  top_p=top_p,
67
  )
68
 
69
- # Prepare inputs
70
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
71
 
72
- # Generate response
73
  try:
74
  response = model.generate_content(inputs, stream=True, generation_config=generation_config)
75
  response.resolve()
76
  except Exception as e:
77
- chatbot[-1][1] = f"Error occurred: {str(e)}"
78
  yield chatbot
79
  return
80
 
81
- # Stream the response back to the chatbot
82
- chatbot[-1][1] = ""
83
  for chunk in response:
84
  for i in range(0, len(chunk.text), 10):
85
- chatbot[-1][1] += chunk.text[i:i + 10]
86
  time.sleep(0.01)
87
  yield chatbot
88
  # Components
@@ -94,7 +91,7 @@ google_key_component = gr.Textbox(
94
  )
95
 
96
  image_prompt_component = gr.Image(type="pil", label="Input Image (Optional: Figure/Graph)")
97
- chatbot_component = gr.Chatbot(label="Chatbot",type="tuples")
98
  text_prompt_component = gr.Textbox(
99
  placeholder="Type your question here...",
100
  label="Ask",
 
25
  image_height = int(image.height * IMAGE_WIDTH / image.width)
26
  return image.resize((IMAGE_WIDTH, image_height))
27
 
28
+ def user(text_prompt: str, chatbot):
29
+ if chatbot is None:
30
+ chatbot = []
31
+ return "", chatbot + [{"role": "user", "content": text_prompt}, {"role": "assistant", "content": ""}]
32
 
33
  def bot(
34
  google_key: str,
 
38
  stop_sequences: str,
39
  top_k: int,
40
  top_p: float,
41
+ chatbot: List[dict]
42
  ):
43
  google_key = google_key or GOOGLE_API_KEY
44
  if not google_key:
45
  raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
46
 
47
+ text_prompt = chatbot[-2]["content"].strip() if len(chatbot) >= 2 and chatbot[-2]["content"] else None
48
 
49
  # Handle cases for text and/or image input
50
  if not text_prompt and not image_prompt:
51
+ chatbot[-1]["content"] = "Prompt cannot be empty. Please provide input text or an image."
52
  yield chatbot
53
  return
54
  elif image_prompt and not text_prompt:
 
55
  text_prompt = "Describe the image"
56
  elif image_prompt and text_prompt:
 
57
  text_prompt = f"{text_prompt}. Also, analyze the provided image."
58
 
59
  # Configure the model
 
66
  top_p=top_p,
67
  )
68
 
 
69
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
70
 
 
71
  try:
72
  response = model.generate_content(inputs, stream=True, generation_config=generation_config)
73
  response.resolve()
74
  except Exception as e:
75
+ chatbot[-1]["content"] = f"Error occurred: {str(e)}"
76
  yield chatbot
77
  return
78
 
79
+ chatbot[-1]["content"] = ""
 
80
  for chunk in response:
81
  for i in range(0, len(chunk.text), 10):
82
+ chatbot[-1]["content"] += chunk.text[i:i + 10]
83
  time.sleep(0.01)
84
  yield chatbot
85
  # Components
 
91
  )
92
 
93
  image_prompt_component = gr.Image(type="pil", label="Input Image (Optional: Figure/Graph)")
94
+ chatbot_component = gr.Chatbot(label="Chatbot")
95
  text_prompt_component = gr.Textbox(
96
  placeholder="Type your question here...",
97
  label="Ask",