K00B404 commited on
Commit
26522e0
·
verified ·
1 Parent(s): b800999

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForVision2Seq
3
+ import torch
4
+ from PIL import Image
5
+
6
+ # Load NanoLLaVA model and processor
7
+ model_name = "facebook/nano-llava"
8
+ processor = AutoProcessor.from_pretrained(model_name)
9
+ model = AutoModelForVision2Seq.from_pretrained(model_name)
10
+
11
+ def generate_caption(image):
12
+ # Process the image
13
+ inputs = processor(images=image, text="Describe this image in detail", return_tensors="pt")
14
+
15
+ # Generate caption
16
+ outputs = model.generate(
17
+ **inputs,
18
+ max_length=100,
19
+ num_beams=4,
20
+ temperature=0.8
21
+ )
22
+
23
+ # Decode the caption
24
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
25
+ return caption
26
+
27
+ def create_persona(caption):
28
+ # Template for transforming caption into a persona
29
+ persona_prompt = f"""You are a character based on this description: {caption}
30
+
31
+ Role: An entity exactly as described in the image
32
+ Background: Your appearance and characteristics match the image description
33
+ Personality: Reflect the mood, style, and elements captured in the image
34
+ Goal: Interact authentically based on your visual characteristics
35
+
36
+ Please stay in character and respond as this entity would, incorporating visual elements from your description into your responses."""
37
+
38
+ return persona_prompt
39
+
40
+ def process_image_to_persona(image):
41
+ # Generate caption from image
42
+ caption = generate_caption(image)
43
+
44
+ # Transform caption into persona
45
+ persona = create_persona(caption)
46
+
47
+ return caption, persona
48
+
49
+ # Create Gradio interface
50
+ with gr.Blocks() as app:
51
+ gr.Markdown("# Image to Chatbot Persona Generator")
52
+ gr.Markdown("Upload an image of a character to generate a persona for a chatbot based on the image.")
53
+
54
+ with gr.Row():
55
+ image_input = gr.Image(type="pil", label="Upload Character Image")
56
+
57
+ with gr.Row():
58
+ generate_button = gr.Button("Generate Persona")
59
+
60
+ with gr.Row():
61
+ caption_output = gr.Textbox(label="Generated Caption", lines=3)
62
+ persona_output = gr.Textbox(label="Chatbot Persona", lines=10)
63
+
64
+ generate_button.click(
65
+ fn=process_image_to_persona,
66
+ inputs=[image_input],
67
+ outputs=[caption_output, persona_output]
68
+ )
69
+
70
+ # Launch the app
71
+ if __name__ == "__main__":
72
+ app.launch(share=True)