mjavaid commited on
Commit
b78ce74
·
1 Parent(s): a05aab1

first commit

Browse files
Files changed (1) hide show
  1. app.py +40 -35
app.py CHANGED
@@ -6,8 +6,7 @@ import os
6
 
7
  hf_token = os.environ["HF_TOKEN"]
8
 
9
- # Load the Gemma 3 pipeline.
10
- # Gemma 3 is a multimodal model that accepts text and image inputs.
11
  pipe = pipeline(
12
  "image-text-to-text",
13
  model="google/gemma-3-4b-it",
@@ -15,54 +14,60 @@ pipe = pipeline(
15
  torch_dtype=torch.bfloat16,
16
  use_auth_token=hf_token
17
  )
 
18
  @spaces.GPU
19
  def generate_response(user_text, user_image, history):
20
- messages = [
21
- {
22
- "role": "system",
23
- "content": [{"type": "text", "text": "You are a helpful assistant."}]
24
- }
25
- ]
 
26
  user_content = []
27
- if user_image is not None:
28
- user_content.append({"type": "image", "image": user_image})
29
  if user_text:
30
  user_content.append({"type": "text", "text": user_text})
31
- messages.append({"role": "user", "content": user_content})
 
32
 
33
- # Call the pipeline with the provided messages.
34
- output = pipe(text=messages, max_new_tokens=200)
35
-
36
- print(output)
37
- print(output[0]["generated_text"][-1]["content"])
38
 
39
- # Attempt to extract the generated content using the expected structure.
40
  try:
 
 
 
 
41
  response = output[0]["generated_text"][-1]["content"]
42
- history.append((user_text, response))
43
-
44
- except (KeyError, IndexError, TypeError):
45
- # Fallback: return the raw output as a string.
46
- #print(response)
47
- pass
48
- #response = str(output)
49
 
50
  return history, history
51
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# Gemma 3 Chat Interface")
54
- gr.Markdown(
55
- "This interface lets you chat with the Gemma 3 model. "
56
- "You can type a message and optionally attach an image."
57
- )
58
- # Specify type="messages" to avoid deprecation warnings.
59
- chatbot = gr.Chatbot(type="messages")
60
  with gr.Row():
61
- txt = gr.Textbox(show_label=False, placeholder="Type your message here...", container=False)
62
- img = gr.Image(type="pil", label="Attach an image (optional)")
 
 
 
63
  state = gr.State([])
64
-
65
- txt.submit(generate_response, inputs=[txt, img, state], outputs=[chatbot, state])
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
- demo.launch()
 
6
 
7
  hf_token = os.environ["HF_TOKEN"]
8
 
9
+ # Load the Gemma 3 pipeline
 
10
  pipe = pipeline(
11
  "image-text-to-text",
12
  model="google/gemma-3-4b-it",
 
14
  torch_dtype=torch.bfloat16,
15
  use_auth_token=hf_token
16
  )
17
+
18
  @spaces.GPU
19
  def generate_response(user_text, user_image, history):
20
+ # Initialize message structure
21
+ messages = [{
22
+ "role": "system",
23
+ "content": [{"type": "text", "text": "You are a helpful assistant."}]
24
+ }]
25
+
26
+ # Build user content
27
  user_content = []
 
 
28
  if user_text:
29
  user_content.append({"type": "text", "text": user_text})
30
+ if user_image is not None:
31
+ user_content.append({"type": "image", "image": user_image})
32
 
33
+ if user_content:
34
+ messages.append({"role": "user", "content": user_content})
 
 
 
35
 
 
36
  try:
37
+ # Generate response
38
+ output = pipe(text=messages, max_new_tokens=200)
39
+
40
+ # Extract generated response
41
  response = output[0]["generated_text"][-1]["content"]
42
+ except Exception as e:
43
+ print(f"Error generating response: {e}")
44
+ response = "Sorry, I encountered an error processing your request."
45
+
46
+ # Update chat history
47
+ if user_text or user_image:
48
+ history.append(((user_text, user_image) if user_image else user_text, response))
49
 
50
  return history, history
51
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# Gemma 3 Chat Interface")
54
+ gr.Markdown("This interface lets you chat with the Gemma 3 model. You can type a message and optionally attach an image.")
55
+
56
+ chatbot = gr.Chatbot()
57
+
 
 
58
  with gr.Row():
59
+ with gr.Column(scale=3):
60
+ txt = gr.Textbox(show_label=False, placeholder="Type your message here...", container=False)
61
+ with gr.Column(scale=1):
62
+ img = gr.Image(type="pil", label="Optional Image Upload")
63
+
64
  state = gr.State([])
65
+
66
+ txt.submit(
67
+ generate_response,
68
+ inputs=[txt, img, state],
69
+ outputs=[chatbot, state]
70
+ )
71
 
72
  if __name__ == "__main__":
73
+ demo.launch()