KABURAKURIA commited on
Commit
f3041c4
·
verified ·
1 Parent(s): fc2a158

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -32
app.py CHANGED
@@ -34,36 +34,39 @@ def classify_image(img):
34
  }
35
 
36
  def respond(
37
- message,
38
- history: list[tuple[str, str]],
39
  system_message,
40
  max_tokens,
41
  temperature,
42
- top_p,
43
  ):
44
- messages = [{"role": "system", "content": system_message}]
45
-
46
- for val in history:
47
- if val[0]:
48
- messages.append({"role": "user", "content": val[0]})
49
- if val[1]:
50
- messages.append({"role": "assistant", "content": val[1]})
51
-
52
- messages.append({"role": "user", "content": message})
53
-
54
- response = ""
55
-
56
- for message in client.chat_completion(
57
- messages,
58
- max_tokens=max_tokens,
59
- stream=True,
60
- temperature=temperature,
61
- top_p=top_p,
62
- ):
63
- token = message.choices[0].delta.content
64
-
65
- response += token
66
- yield response
 
 
 
 
67
 
68
  def emotion_and_chat(img, system_message, max_tokens, temperature, top_p):
69
  # Classify the image to detect emotion
@@ -72,8 +75,10 @@ def emotion_and_chat(img, system_message, max_tokens, temperature, top_p):
72
 
73
  # Start chatbot conversation based on the detected emotion
74
  initial_message = f"I detected that you're feeling {detected_emotion}. Let's talk about it."
75
- chat_history = [(initial_message, None)] # Start conversation with detected emotion
76
- return respond(initial_message, chat_history, system_message, max_tokens, temperature, top_p)
 
 
77
 
78
  # Define custom CSS for styling
79
  custom_css = """
@@ -136,9 +141,9 @@ body {
136
 
137
  # Define example images from URLs
138
  examples = [
139
- ["https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/a-captivating-ukiyo-e-inspired-poster-featuring-a--wTg7L-f2Tfiy6K8w6aWnKA-KbGU9GSKSDGBbbxrCO65Mg.jpeg?alt=media&token=64590de9-e265-44ac-a766-aeecd455ed5d"],
140
- ["https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/poster-ai-themed-kenyan-female-silhoutte-written-l-PMIXpNWGQ8KaNNetQRVJuQ-B1TteyL-S5OTPZFXvfGybg.jpeg?alt=media&token=fc10f96d-403e-4f75-bd9c-810e0da36867"],
141
- ["https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/poster-ai-themed-kenyan-male-silhoutte-written-log-z3fqBD5bQOOj6uqGd_iXLQ-4aBfNy0ZTgmLlTsZh1dzIA.jpeg?alt=media&token=f218f160-d38e-482f-97a9-5442c2f251a7"]
142
  ]
143
 
144
  # Gradio Interface
@@ -160,4 +165,5 @@ interface = gr.Interface(
160
  )
161
 
162
  # Launch the Gradio interface
163
- interface.launch()
 
 
34
  }
35
 
36
  def respond(
37
+ messages,
 
38
  system_message,
39
  max_tokens,
40
  temperature,
41
+ top_p
42
  ):
43
+ # Ensure messages are correctly formatted
44
+ formatted_messages = []
45
+ for message in messages:
46
+ if message['content'] is None:
47
+ message['content'] = '' # Set to empty string if None
48
+ formatted_messages.append(message)
49
+
50
+ # Add system message at the beginning
51
+ formatted_messages.insert(0, {"role": "system", "content": system_message})
52
+
53
+ # Proceed with chat completion
54
+ try:
55
+ response = ""
56
+ for message in client.chat_completion(
57
+ model_id='HuggingFaceH4/zephyr-7b-beta',
58
+ messages=formatted_messages,
59
+ max_tokens=max_tokens,
60
+ temperature=temperature,
61
+ top_p=top_p,
62
+ stream=True
63
+ ):
64
+ token = message.choices[0].delta.content
65
+ response += token
66
+ return response
67
+ except Exception as e:
68
+ print(f"Error: {e}")
69
+ return "Sorry, there was an error processing your request."
70
 
71
  def emotion_and_chat(img, system_message, max_tokens, temperature, top_p):
72
  # Classify the image to detect emotion
 
75
 
76
  # Start chatbot conversation based on the detected emotion
77
  initial_message = f"I detected that you're feeling {detected_emotion}. Let's talk about it."
78
+ chat_history = [{"role": "user", "content": initial_message}]
79
+ chat_response = respond(chat_history, system_message, max_tokens, temperature, top_p)
80
+
81
+ return chat_response
82
 
83
  # Define custom CSS for styling
84
  custom_css = """
 
141
 
142
  # Define example images from URLs
143
  examples = [
144
+ "https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/a-captivating-ukiyo-e-inspired-poster-featuring-a--wTg7L-f2Tfiy6K8w6aWnKA-KbGU9GSKSDGBbbxrCO65Mg.jpeg?alt=media&token=64590de9-e265-44ac-a766-aeecd455ed5d",
145
+ "https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/poster-ai-themed-kenyan-female-silhoutte-written-l-PMIXpNWGQ8KaNNetQRVJuQ-B1TteyL-S5OTPZFXvfGybg.jpeg?alt=media&token=fc10f96d-403e-4f75-bd9c-810e0da36867",
146
+ "https://firebasestorage.googleapis.com/v0/b/hisia-4b65b.appspot.com/o/poster-ai-themed-kenyan-male-silhoutte-written-log-z3fqBD5bQOOj6uqGd_iXLQ-4aBfNy0ZTgmLlTsZh1dzIA.jpeg?alt=media&token=f218f160-d38e-482f-97a9-5442c2f251a7"
147
  ]
148
 
149
  # Gradio Interface
 
165
  )
166
 
167
  # Launch the Gradio interface
168
+ if __name__ == "__main__":
169
+ interface.launch()