docilio commited on
Commit
fcea4c9
Β·
1 Parent(s): 4c31195

update application file

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from transformers import AutoProcessor, Gemma3nForConditionalGeneration
3
- from PIL import Image
4
  import torch
5
  import textwrap
6
 
@@ -17,20 +16,11 @@ model = Gemma3nForConditionalGeneration.from_pretrained(
17
  def print_response(text: str) -> str:
18
  return "\n".join(textwrap.fill(line, 100) for line in text.split("\n"))
19
 
20
- # πŸ” Inference function
21
- def predict(image: Image.Image, instruction: str) -> str:
22
  messages = [
23
- {
24
- "role": "system",
25
- "content": [{"type": "text", "text": "You are a helpful assistant that extracts fields from documents."}],
26
- },
27
- {
28
- "role": "user",
29
- "content": [
30
- {"type": "image", "image": image},
31
- {"type": "text", "text": instruction}
32
- ],
33
- },
34
  ]
35
 
36
  inputs = processor.apply_chat_template(
@@ -57,14 +47,14 @@ def predict(image: Image.Image, instruction: str) -> str:
57
 
58
  # πŸŽ›οΈ Gradio Interface
59
  demo = gr.Interface(
60
- fn=predict,
61
  inputs=[
62
- gr.Image(type="pil", label="Upload Image"),
63
- gr.Textbox(lines=2, label="Instruction", value="List visible fields as JSON array {field, value}.")
64
  ],
65
- outputs=gr.Textbox(label="Output"),
66
- title="Gemma 3n Vision + Text",
67
- description="Upload an image (e.g., a passport or form) and ask for structured output."
68
  )
69
 
70
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import AutoProcessor, Gemma3nForConditionalGeneration
 
3
  import torch
4
  import textwrap
5
 
 
16
  def print_response(text: str) -> str:
17
  return "\n".join(textwrap.fill(line, 100) for line in text.split("\n"))
18
 
19
+ # πŸ” Inference function for text-only input
20
+ def predict_text(system_prompt: str, user_prompt: str) -> str:
21
  messages = [
22
+ {"role": "system", "content": [{"type": "text", "text": system_prompt.strip()}]},
23
+ {"role": "user", "content": [{"type": "text", "text": user_prompt.strip()}]},
 
 
 
 
 
 
 
 
 
24
  ]
25
 
26
  inputs = processor.apply_chat_template(
 
47
 
48
  # πŸŽ›οΈ Gradio Interface
49
  demo = gr.Interface(
50
+ fn=predict_text,
51
  inputs=[
52
+ gr.Textbox(lines=2, label="System Prompt", value="You are a helpful assistant."),
53
+ gr.Textbox(lines=4, label="User Prompt", placeholder="Ask something..."),
54
  ],
55
+ outputs=gr.Textbox(label="Gemma 3n Response"),
56
+ title="Gemma 3n Text-Only Chat",
57
+ description="Interact with the Gemma 3n language model using plain text. Image input not required.",
58
  )
59
 
60
  if __name__ == "__main__":