update
Browse files
app.py
CHANGED
|
@@ -23,8 +23,8 @@ def preprocess_image(image: Image.Image) -> Image.Image:
|
|
| 23 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 24 |
return image.resize((IMAGE_WIDTH, image_height))
|
| 25 |
|
| 26 |
-
def user(text_prompt: str, chatbot: List
|
| 27 |
-
return "", chatbot + [
|
| 28 |
|
| 29 |
def bot(
|
| 30 |
google_key: str,
|
|
@@ -34,17 +34,17 @@ def bot(
|
|
| 34 |
stop_sequences: str,
|
| 35 |
top_k: int,
|
| 36 |
top_p: float,
|
| 37 |
-
chatbot: List
|
| 38 |
):
|
| 39 |
google_key = google_key or GOOGLE_API_KEY
|
| 40 |
if not google_key:
|
| 41 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 42 |
|
| 43 |
-
text_prompt = chatbot[-1][
|
| 44 |
|
| 45 |
# Handle cases for text and/or image input
|
| 46 |
if not text_prompt and not image_prompt:
|
| 47 |
-
chatbot[-1][
|
| 48 |
yield chatbot
|
| 49 |
return
|
| 50 |
elif image_prompt and not text_prompt:
|
|
@@ -74,17 +74,18 @@ def bot(
|
|
| 74 |
response = model_instance.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 75 |
response.resolve()
|
| 76 |
except Exception as e:
|
| 77 |
-
chatbot[-1][
|
| 78 |
yield chatbot
|
| 79 |
return
|
| 80 |
|
| 81 |
# Stream the response back to the chatbot
|
| 82 |
-
chatbot
|
| 83 |
for chunk in response:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
# Components
|
| 89 |
google_key_component = gr.Textbox(
|
| 90 |
label="Google API Key",
|
|
|
|
| 23 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 24 |
return image.resize((IMAGE_WIDTH, image_height))
|
| 25 |
|
| 26 |
+
def user(text_prompt: str, chatbot: List):
|
| 27 |
+
return "", chatbot + [{"role": "user", "content": text_prompt}]
|
| 28 |
|
| 29 |
def bot(
|
| 30 |
google_key: str,
|
|
|
|
| 34 |
stop_sequences: str,
|
| 35 |
top_k: int,
|
| 36 |
top_p: float,
|
| 37 |
+
chatbot: List
|
| 38 |
):
|
| 39 |
google_key = google_key or GOOGLE_API_KEY
|
| 40 |
if not google_key:
|
| 41 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 42 |
|
| 43 |
+
text_prompt = chatbot[-1]["content"].strip() if chatbot[-1]["content"] else None
|
| 44 |
|
| 45 |
# Handle cases for text and/or image input
|
| 46 |
if not text_prompt and not image_prompt:
|
| 47 |
+
chatbot[-1]["content"] = "Prompt cannot be empty. Please provide input text or an image."
|
| 48 |
yield chatbot
|
| 49 |
return
|
| 50 |
elif image_prompt and not text_prompt:
|
|
|
|
| 74 |
response = model_instance.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 75 |
response.resolve()
|
| 76 |
except Exception as e:
|
| 77 |
+
chatbot[-1]["content"] = f"Error occurred: {str(e)}"
|
| 78 |
yield chatbot
|
| 79 |
return
|
| 80 |
|
| 81 |
# Stream the response back to the chatbot
|
| 82 |
+
chatbot.append({"role": "assistant", "content": ""})
|
| 83 |
for chunk in response:
|
| 84 |
+
if chunk.text:
|
| 85 |
+
for i in range(0, len(chunk.text), 10):
|
| 86 |
+
chatbot[-1]["content"] += chunk.text[i:i + 10]
|
| 87 |
+
time.sleep(0.01)
|
| 88 |
+
yield chatbot
|
| 89 |
# Components
|
| 90 |
google_key_component = gr.Textbox(
|
| 91 |
label="Google API Key",
|