jethrovic commited on
Commit
cfe3102
·
1 Parent(s): 8d806f8

Create app.py

Browse files

updates iya yin

Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_client import Client
3
+
4
+ #fusecap_client = Client("https://noamrot-fusecap-image-captioning.hf.space/")
5
+ fuyu_client = Client("https://adept-fuyu-8b-demo.hf.space/")
6
+
7
+ def get_caption(image_in):
8
+
9
+ fuyu_result = fuyu_client.predict(
10
+ image_in, # str representing input in 'raw_image' Image component
11
+ True, # bool in 'Enable detailed captioning' Checkbox component
12
+ fn_index=2
13
+ )
14
+
15
+ # Find the last occurrence of "."
16
+ last_period_index = fuyu_result.rfind('.')
17
+
18
+ # Truncate the string up to the last period
19
+ truncated_caption = fuyu_result[:last_period_index + 1]
20
+
21
+ # print(truncated_caption)
22
+ print(f"\n—\nIMAGE CAPTION: {truncated_caption}")
23
+
24
+ return truncated_caption
25
+
26
+
27
+ def infer(image_in):
28
+ gr.Info("Getting image caption with Fuyu...")
29
+ user_prompt = get_caption(image_in)
30
+
31
+
32
+
33
+ return user_prompt
34
+
35
+ title = f"LLM Agent from a Picture",
36
+ description = f"Get a LLM system prompt from a picture so you can use it in <a href='https://huggingface.co/spaces/abidlabs/GPT-Baker'>GPT-Baker</a>."
37
+
38
+ css = """
39
+ #col-container{
40
+ margin: 0 auto;
41
+ max-width: 780px;
42
+ text-align: left;
43
+ }
44
+ """
45
+
46
+ with gr.Blocks(css=css) as demo:
47
+ with gr.Column(elem_id="col-container"):
48
+ gr.HTML(f"""
49
+ <h2 style="text-align: center;">LLM Agent from a Picture</h2>
50
+ <p style="text-align: center;">{description}</p>
51
+ """)
52
+
53
+ with gr.Row():
54
+ with gr.Column():
55
+ image_in = gr.Image(
56
+ label = "Image reference",
57
+ type = "filepath",
58
+ elem_id = "image-in"
59
+ )
60
+ submit_btn = gr.Button("Make desciptions of my pic !")
61
+ with gr.Column():
62
+ result = gr.Textbox(
63
+ label ="Suggested System",
64
+ lines = 6,
65
+ max_lines = 30,
66
+ elem_id = "suggested-system-prompt"
67
+ )
68
+ with gr.Row():
69
+ gr.Examples(
70
+ examples = [
71
+ ["ponder.png"],
72
+ ["ponder2.png"],
73
+
74
+ ],
75
+ fn = infer,
76
+ inputs = [image_in],
77
+ outputs = [result],
78
+ cache_examples = True
79
+ )
80
+
81
+ submit_btn.click(
82
+ fn = infer,
83
+ inputs = [
84
+ image_in
85
+ ],
86
+ outputs =[
87
+ result
88
+ ]
89
+ )
90
+
91
+ demo.queue().launch(show_api=False)