File size: 2,619 Bytes
cf2752b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# load dependencies
import transformers
import torch
import gradio as gr
from transformers import AutoModelForCausalLM,AutoTokenizer
from transformers import pipeline
import time
def pred_on_text(input_text):
  start_time=time.time()
  raw_output = loaded_model_pipeline(text_inputs=[{"role":"user",
                                                   "content":input_text}],
                                     max_new_tokens=256,
                                     disable_compile=True)
  end_time = time.time()
  total_time = round(end_time - start_time,4)
  generated_text = raw_output[0]["generated_text"][1]["content"]
  return generated_text,raw_output,total_time

# load the model (from our hugging face repo)


MODEL_PATH = "csvis/food-extract-gemma-3-270m-finetune-v1"
# load the model into pipeline

loaded_model=AutoModelForCausalLM.from_pretrained(
    pretrained_model_name_or_path = MODEL_PATH,
    dtype="auto",
    device_map="auto",
    attn_implementation="eager"
)
tokenizer= AutoTokenizer.from_pretrained(
    MODEL_PATH
)
# Create model pipeline
loaded_model_pipeline=pipeline("text-generation",
                               model=loaded_model,
                               tokenizer=tokenizer)

# create the demo 
description = """Extract food and drinks items from text with a fine-tuned SLM(small language model)
* Input(str) : Raw text strings or image captions (e.g . "A photo of dog sitting on a beach" or "A breakfast plate wit bacon, eggs and toast")
* Output(str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
For example :
Input: "For breakfast I had eggs, bread and a banana"
Output: food_or_drink: 1
tags: fi
foods: eggs, bread, banana
drinks:"""

demo = gr.Interface(fn=pred_on_text,
                    inputs=gr.TextArea(lines=4,label="Input Text"),
                    outputs=[gr.TextArea(lines=4,label="Generated Text"),
                             gr.TextArea(lines=7,label ="Raw Output"),
                             gr.Number(label="Generation Time (s)")],
                    title="🥭🍎Structured FoodExtract with a Fine-TunedGemma 3 270M",
                    description=description,
                    examples=[["Hello This is my Second fine-tuned LLm!"],
                              ["A plate of food with grilled tuna,salad with avocados,olives,tomatoes and carrot and Italian dressing"],
                              ["Chicken wings"],
                              ["She was Looking beautiful Yesterday at the event"]]
  )


if __name__=="__main__":
  demo.launch(share=True)