leilaaaaa commited on
Commit
cd63ca1
1 Parent(s): 51b8430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -16,35 +16,38 @@ def respond(
16
  top_p,
17
  image=None
18
  ):
19
- try:
20
- messages = [{"role": "system", "content": system_message}]
21
 
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
 
28
- messages.append({"role": "user", "content": message})
29
 
30
- if image:
31
- # Handle single image input
32
- image_b64 = image_to_base64(image) # Convert image to base64
33
- messages.append({"role": "user", "content": "Image uploaded", "image": image_b64})
34
 
35
- # Call Hugging Face model for response
36
- response = ""
37
- for message in client.chat_completion(
38
- messages,
39
- max_tokens=max_tokens,
40
- stream=True,
41
- temperature=temperature,
42
- top_p=top_p,
43
- ):
44
- token = message.choices[0].delta.content
 
 
 
 
 
 
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)}")