update application file
Browse files
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
|
22 |
messages = [
|
23 |
-
{
|
24 |
-
|
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=
|
61 |
inputs=[
|
62 |
-
gr.
|
63 |
-
gr.Textbox(lines=
|
64 |
],
|
65 |
-
outputs=gr.Textbox(label="
|
66 |
-
title="Gemma 3n
|
67 |
-
description="
|
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__":
|