aifeifei798 commited on
Commit
920ecec
·
verified ·
1 Parent(s): 5820d73

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +119 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import copy
4
+ from llama_cpp import Llama
5
+ from huggingface_hub import hf_hub_download
6
+ from transformers import AutoProcessor, AutoModelForCausalLM
7
+ #import spaces
8
+ import re
9
+ from PIL import Image
10
+ import io
11
+
12
+ import subprocess
13
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
14
+
15
+ model = AutoModelForCausalLM.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True).to("cpu").eval()
16
+ processor = AutoProcessor.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True)
17
+
18
+ llm = Llama(
19
+ model_path=hf_hub_download(
20
+ repo_id=os.environ.get("REPO_ID", "ZeroWw/llama3-8B-DarkIdol-2.2-Uncensored-1048K-GGUF"),
21
+ filename=os.environ.get("MODEL_FILE", "llama3-8B-DarkIdol-2.2-Uncensored-1048K.q5_k.gguf"),
22
+ ),
23
+ n_ctx=2048,
24
+ n_gpu_layers=100, # change n_gpu_layers if you have more or less VRAM
25
+ )
26
+
27
+
28
+ def run_pic(image):
29
+ image = Image.open(image[0])
30
+ task_prompt = "<DESCRIPTION>"
31
+ prompt = task_prompt + "Describe this image in great detail."
32
+
33
+ # Ensure the image is in RGB mode
34
+ if image.mode != "RGB":
35
+ image = image.convert("RGB")
36
+
37
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to("cpu")
38
+ generated_ids = model.generate(
39
+ input_ids=inputs["input_ids"],
40
+ pixel_values=inputs["pixel_values"],
41
+ max_new_tokens=1024,
42
+ num_beams=3
43
+ )
44
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
45
+ parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
46
+ return parsed_answer["<DESCRIPTION>"]
47
+
48
+ def generate_text(
49
+ message,
50
+ history: list[tuple[str, str]],
51
+ system_message,
52
+ max_tokens,
53
+ temperature,
54
+ top_p,
55
+ ):
56
+ in_text = message['text']
57
+ in_files = message['files']
58
+ output=""
59
+ if in_files:
60
+ output=run_pic(in_files)
61
+ yield output
62
+ else:
63
+ temp = ""
64
+ input_prompt = f'{system_message}'
65
+ # for interaction in history:
66
+ # input_prompt = input_prompt + str(interaction[0]) + str(interaction[1])
67
+
68
+ input_prompt = input_prompt + " " + str(in_text)
69
+ output = llm(
70
+ input_prompt,
71
+ temperature=temperature,
72
+ top_p=top_p,
73
+ top_k=40,
74
+ repeat_penalty=1.1,
75
+ max_tokens=max_tokens,
76
+ stop=[
77
+ "<|prompter|>",
78
+ "<|endoftext|>",
79
+ "<|endoftext|> \n",
80
+ "ASSISTANT:",
81
+ "USER:",
82
+ "SYSTEM:",
83
+ "<|start_header_id|>",
84
+ "<|eot_id|>",
85
+ ],
86
+ stream=True,
87
+ )
88
+ for out in output:
89
+ stream = copy.deepcopy(out)
90
+ temp += stream["choices"][0]["text"]
91
+ yield temp
92
+
93
+
94
+ demo = gr.ChatInterface(
95
+ generate_text,
96
+ multimodal=True,
97
+ title="Florence-DarkIdol",
98
+ cache_examples=False,
99
+ retry_btn=None,
100
+ undo_btn="Delete Previous",
101
+ clear_btn="Clear",
102
+ additional_inputs=[
103
+ gr.Textbox(value="you are Nagano Natsuki,name:Nagano Natsuki Gender: Female Age: 25 years old Occupation: Adult Video (AV) Actress & Model Personality: Cheerful, optimistic, sometimes naughty; skilled at interacting with audiences.Interests: Drinking, traveling, photography, singing, dancing Expertise: Performing in sexual scenes; well-versed in Japanese language and culture; familiar with various sex techniques. Special Identity Attributes: Renowned AV actress in Japan; nicknamed 'Talent Magician' and 'Princess of Lust'; has a large number of devoted fans. Skills: Acting in pornographic scenes, singing, dancing, photography, swimming; skilled at interacting with audiences.Equipment: Various provocative clothing and shoes; high-quality photography equipment", label="System message"),
104
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
105
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.5, step=0.1, label="Temperature"),
106
+ gr.Slider(
107
+ minimum=0.1,
108
+ maximum=1.0,
109
+ value=0.95,
110
+ step=0.05,
111
+ label="Top-p (nucleus sampling)",
112
+ ),
113
+
114
+ ],
115
+ )
116
+
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch(server_name="0.0.0.0")
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ timm
3
+ llama-cpp-python
4
+ gradio
5
+ huggingface_hub
6
+ # spaces