Quentin GALLOUÉDEC commited on
Commit
e80cdb3
1 Parent(s): 8833512
Files changed (1) hide show
  1. app.py +54 -4
app.py CHANGED
@@ -1,7 +1,57 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from gradio.components import Dropdown, Image, Textbox
4
+ from huggingface_hub import HfApi, ModelFilter
5
+ from transformers import AutoModelForCausalLM, AutoProcessor
6
 
7
+ # Get the list of models from the Hugging Face Hub
8
+ api = HfApi()
9
+ models_infos = api.list_models(author="jat-project", filter=ModelFilter(tags="text-generation"))
10
+ models_names = [model.modelId for model in models_infos]
11
 
12
+ # Dictionary to store loaded models and their pipelines
13
+ models = {}
14
+
15
+ # Load a default model initially
16
+ default_model_name = "jat-project/jat2-small-untrained"
17
+
18
+ def generate_text(model_name, input_image):
19
+ # Check if the selected model is already loaded
20
+ if model_name not in models:
21
+ # Inform the user that the model is loading
22
+ yield "Loading model..."
23
+
24
+ # Load the model
25
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True, padding_side='left')
26
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
27
+ models[model_name] = model, processor
28
+
29
+ # Get the model for the selected model
30
+ model, processor = models[model_name]
31
+
32
+ # Inform the user that the text is being generated
33
+ yield "Generating caption..."
34
+
35
+ # Convert the input image to a tensor
36
+ pixel_values = processor(images=input_image, return_tensors="pt").pixel_values
37
+
38
+ # Generate text
39
+ generated_ids = model.generate(pixel_values=pixel_values, max_length=100, early_stopping=True)
40
+ generated_text = processor.decode(generated_ids[0], skip_special_tokens=True)
41
+
42
+ # Return the generated text
43
+ yield generated_text
44
+
45
+ # Define the Gradio interface
46
+ iface = gr.Interface(
47
+ fn=generate_text, # Function to be called on user input
48
+ inputs=[
49
+ Dropdown(models_names, label="Select Model", value=default_model_name), # Select model
50
+ Image(label="Input Image"), # Image input
51
+ ],
52
+ outputs=Textbox(label="Generated Caption"), # Textbox to display the generated text
53
+ title="JAT Image Captioning", # Title of the interface
54
+ )
55
+
56
+ # Launch the Gradio interface
57
+ iface.launch(enable_queue=True)