Update app.py
Browse files
app.py
CHANGED
@@ -16,35 +16,38 @@ def respond(
|
|
16 |
top_p,
|
17 |
image=None
|
18 |
):
|
19 |
-
|
20 |
-
messages = [{"role": "system", "content": system_message}]
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
response += token
|
47 |
-
yield response
|
48 |
|
49 |
except Exception as e:
|
50 |
print(f"Error in respond function: {str(e)}")
|
|
|
16 |
top_p,
|
17 |
image=None
|
18 |
):
|
19 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
20 |
|
21 |
+
for val in history:
|
22 |
+
if val[0]:
|
23 |
+
messages.append({"role": "user", "content": val[0]})
|
24 |
+
if val[1]:
|
25 |
+
messages.append({"role": "assistant", "content": val[1]})
|
26 |
|
27 |
+
messages.append({"role": "user", "content": message})
|
28 |
|
29 |
+
if image:
|
30 |
+
# Convert image to base64
|
31 |
+
image_b64 = image_to_base64(image)
|
32 |
+
messages.append({"role": "user", "content": "Image uploaded", "image": image_b64})
|
33 |
|
34 |
+
# Call Hugging Face model for response
|
35 |
+
responses = []
|
36 |
+
for message in client.chat_completion(
|
37 |
+
messages,
|
38 |
+
max_tokens=max_tokens,
|
39 |
+
stream=True,
|
40 |
+
temperature=temperature,
|
41 |
+
top_p=top_p,
|
42 |
+
):
|
43 |
+
token = message.choices[0].delta.content
|
44 |
+
|
45 |
+
responses.append(token)
|
46 |
+
|
47 |
+
# Yield each response sequentially
|
48 |
+
for response in responses:
|
49 |
+
yield response
|
50 |
|
|
|
|
|
51 |
|
52 |
except Exception as e:
|
53 |
print(f"Error in respond function: {str(e)}")
|