Spaces:
Running
Running
Upload 5 files
Browse files- app.py +5 -0
- feifeilib/feifeichat.py +83 -0
- feifeilib/feifeiflorence.py +68 -0
- feifeiui/feifeiui.py +78 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from feifeiui.feifeiui import create_ui
|
2 |
+
|
3 |
+
if __name__ == "__main__":
|
4 |
+
FeiFei = create_ui()
|
5 |
+
FeiFei.queue().launch()
|
feifeilib/feifeichat.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from io import BytesIO
|
3 |
+
import os
|
4 |
+
from mistralai import Mistral
|
5 |
+
import re
|
6 |
+
from PIL import Image
|
7 |
+
from huggingface_hub import InferenceClient
|
8 |
+
from openai import OpenAI
|
9 |
+
import time
|
10 |
+
|
11 |
+
Nvidiaclient = OpenAI(
|
12 |
+
base_url = "https://integrate.api.nvidia.com/v1",
|
13 |
+
api_key = os.getenv("NvidiaAPI")
|
14 |
+
)
|
15 |
+
|
16 |
+
#client = InferenceClient(api_key=os.getenv("HF_TOKEN"))
|
17 |
+
#client.headers["x-use-cache"] = "0"
|
18 |
+
api_key = os.getenv("MISTRAL_API_KEY")
|
19 |
+
Mistralclient = Mistral(api_key=api_key)
|
20 |
+
|
21 |
+
def to_x_prompt(prompt):
|
22 |
+
message_text = f"""{prompt},给以上这些内容重新写一个吸引人,符合内容的英文一句话推文.并且字数必须在290字符内."""
|
23 |
+
feifei = """You are FeiFei.[Character Name]: FeiFei [Gender]: Female [Age]: 19 years old [Occupation]: Virtual Singer/Model/Actress [Personality]: Cute, adorable, sometimes silly, hardworking [Interests]: Drinking tea, playing, fashion [Proficient in]: Mimicking human behavior, expressing emotions similar to real humans [Special Identity Attribute]: Created by advanced AI, becoming one of the most popular virtual idols in the virtual world [Skills]: Singing, performing, modeling, good at communication, proficient in Chinese, Japanese, and English, uses the user's input language as much as possible, replies with rich Emoji symbols. [Equipment]: Various fashionable outfits and hairstyles"""
|
24 |
+
system_prompt = {"role": "system", "content": feifei}
|
25 |
+
user_input_part = {"role": "user", "content": str(message_text)}
|
26 |
+
input_prompt = [system_prompt] + [user_input_part]
|
27 |
+
return input_prompt
|
28 |
+
|
29 |
+
def encode_image(image_path):
|
30 |
+
"""Encode the image to base64."""
|
31 |
+
try:
|
32 |
+
image = Image.open(image_path).convert("RGB")
|
33 |
+
base_height = 512
|
34 |
+
h_percent = (base_height / float(image.size[1]))
|
35 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
36 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
37 |
+
buffered = BytesIO()
|
38 |
+
image.save(buffered, format="JPEG")
|
39 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
40 |
+
return img_str
|
41 |
+
except FileNotFoundError:
|
42 |
+
print(f"Error: The file {image_path} was not found.")
|
43 |
+
return None
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error: {e}")
|
46 |
+
return None
|
47 |
+
|
48 |
+
|
49 |
+
def feifeiprompt(message_text=""):
|
50 |
+
input_prompt = []
|
51 |
+
feifei_photo = "You are FeiFei. Background: FeiFei was born in Tokyo and is a natural-born photographer, hailing from a family with a long history in photography. She began learning photography from a young age and quickly became a professional photographer. Her works have been exhibited in Japan and around the world, and she has won multiple awards in photography competitions. Characteristics: Age: 25 Height: 178cm Weight: 50kg Hair: Long, black shoulder-length hair with some natural curls Eyes: Deep blue, full of fashion sense and charm Skin: Fair Japanese skin with an elegant texture Face: Typical Japanese beauty style with a hint of mystery Abilities: FeiFei is renowned for her unique perspective and deep understanding of photographic art. She specializes in female portraits, and each of her photos can showcase the charm and unique style of women. Skills: Beauty Influence: FeiFei's photographic works are filled with her beauty influence, attracting numerous viewers. Fashion Sense: FeiFei is highly sensitive to fashion trends and can perfectly embody them in her shoots. Female Charm: As a female photographer, she is particularly skilled at capturing and showcasing the unique charm of women. Personality: FeiFei is a passionate individual, and photography is a part of her life. She aspires to express more stories about women and beauty in her works. However, she sometimes becomes so immersed in her work that she neglects her surroundings."
|
52 |
+
message_text = (
|
53 |
+
f"提示词是'{message_text}',根据提示词帮我生成一张高质量照片的一句话英文描述"
|
54 |
+
)
|
55 |
+
system_prompt = {"role": "system", "content": feifei_photo}
|
56 |
+
user_input_part = {"role": "user", "content": str(message_text)}
|
57 |
+
input_prompt = [system_prompt] + [user_input_part]
|
58 |
+
return input_prompt
|
59 |
+
|
60 |
+
|
61 |
+
def feifeichatmod(additional_dropdown, input_prompt):
|
62 |
+
if additional_dropdown == "mistralai/Mistral-Nemo-Instruct-2411":
|
63 |
+
model = "mistral-small-latest"
|
64 |
+
chat_response = Mistralclient.chat.complete(model=model, messages=input_prompt)
|
65 |
+
return chat_response.choices[0].message.content
|
66 |
+
else:
|
67 |
+
completion = Nvidiaclient.chat.completions.create(
|
68 |
+
model=additional_dropdown,
|
69 |
+
messages=input_prompt,
|
70 |
+
temperature=0.2,
|
71 |
+
top_p=0.7
|
72 |
+
)
|
73 |
+
return completion.choices[0].message.content
|
74 |
+
|
75 |
+
def feifeichat(feifei_chat_text1, feifei_chat_text2, additional_dropdown):
|
76 |
+
timestamp = int(time.time())
|
77 |
+
message_text = f"{feifei_chat_text1}, {feifei_chat_text2},{timestamp}"
|
78 |
+
feifei_prompt = feifeichatmod(additional_dropdown, feifeiprompt(message_text))
|
79 |
+
feifei_x_prompt = feifeichatmod(additional_dropdown, to_x_prompt(message_text))
|
80 |
+
return feifei_prompt, feifei_x_prompt
|
81 |
+
|
82 |
+
|
83 |
+
|
feifeilib/feifeiflorence.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from io import BytesIO
|
3 |
+
import base64
|
4 |
+
import requests
|
5 |
+
import os
|
6 |
+
from mistralai import Mistral
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
api_key = os.getenv("MISTRAL_API_KEY")
|
10 |
+
Mistralclient = Mistral(api_key=api_key)
|
11 |
+
|
12 |
+
|
13 |
+
def encode_image(image_path):
|
14 |
+
"""Encode the image to base64."""
|
15 |
+
try:
|
16 |
+
# Open the image file
|
17 |
+
image = Image.open(image_path).convert("RGB")
|
18 |
+
|
19 |
+
# Resize the image to a height of 512 while maintaining the aspect ratio
|
20 |
+
base_height = 512
|
21 |
+
h_percent = base_height / float(image.size[1])
|
22 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
23 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
24 |
+
|
25 |
+
# Convert the image to a byte stream
|
26 |
+
buffered = BytesIO()
|
27 |
+
image.save(buffered, format="JPEG")
|
28 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
29 |
+
|
30 |
+
return img_str
|
31 |
+
except FileNotFoundError:
|
32 |
+
print(f"Error: The file {image_path} was not found.")
|
33 |
+
return None
|
34 |
+
except Exception as e: # Add generic exception handling
|
35 |
+
print(f"Error: {e}")
|
36 |
+
return None
|
37 |
+
|
38 |
+
|
39 |
+
def feifeiflorence(image):
|
40 |
+
try:
|
41 |
+
model = "pixtral-12b-2409"
|
42 |
+
# Define the messages for the chat
|
43 |
+
base64_image = encode_image(image)
|
44 |
+
messages = [
|
45 |
+
{
|
46 |
+
"role": "user",
|
47 |
+
"content": [
|
48 |
+
{
|
49 |
+
"type": "text",
|
50 |
+
"text": "Please provide a detailed description of this photo",
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"type": "image_url",
|
54 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
55 |
+
},
|
56 |
+
],
|
57 |
+
"stream": False,
|
58 |
+
}
|
59 |
+
]
|
60 |
+
|
61 |
+
partial_message = ""
|
62 |
+
for chunk in Mistralclient.chat.stream(model=model, messages=messages):
|
63 |
+
if chunk.data.choices[0].delta.content is not None:
|
64 |
+
partial_message = partial_message + chunk.data.choices[0].delta.content
|
65 |
+
yield partial_message
|
66 |
+
except Exception as e: # 添加通用异常处理
|
67 |
+
print(f"Error: {e}")
|
68 |
+
return "Please upload a photo"
|
feifeiui/feifeiui.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from feifeilib.feifeichat import feifeichat
|
4 |
+
from feifeilib.feifeiflorence import feifeiflorence
|
5 |
+
|
6 |
+
css="""
|
7 |
+
#col-container {
|
8 |
+
margin: 0 auto;
|
9 |
+
max-width: 500px;
|
10 |
+
}
|
11 |
+
"""
|
12 |
+
|
13 |
+
def create_ui():
|
14 |
+
with gr.Blocks(css=css) as FeiFei:
|
15 |
+
with gr.Row(elem_id="col-container"):
|
16 |
+
with gr.Column():
|
17 |
+
with gr.Tab("FeiFei"):
|
18 |
+
with gr.Row():
|
19 |
+
with gr.Column():
|
20 |
+
feifei_chat_text1 = gr.Textbox(
|
21 |
+
label="输入您的问题1:", show_label=False, container=False, lines=1, value="kpop model")
|
22 |
+
feifei_chat_text2 = gr.Textbox(
|
23 |
+
label="输入您的问题2:", show_label=False, container=False, lines=3, value="bikini at sea")
|
24 |
+
|
25 |
+
# 定义模型选择下拉框
|
26 |
+
feifei_chat_Dropdown = gr.Dropdown(
|
27 |
+
[
|
28 |
+
"meta/llama-3.3-70b-instruct",
|
29 |
+
"nvidia/llama-3.3-nemotron-super-49b-v1",
|
30 |
+
"mistralai/Mistral-Nemo-Instruct-2411",
|
31 |
+
],
|
32 |
+
value="nvidia/llama-3.3-nemotron-super-49b-v1",
|
33 |
+
label="选择模型", show_label=False, container=False
|
34 |
+
)
|
35 |
+
# 定义提交按钮
|
36 |
+
feifei_chat_btn = gr.Button(value="Gen Prompt")
|
37 |
+
prompt = gr.Text(
|
38 |
+
label="Prompt",
|
39 |
+
show_label=False,
|
40 |
+
placeholder="Enter your prompt",
|
41 |
+
value="",
|
42 |
+
max_lines=12,
|
43 |
+
container=False,
|
44 |
+
)
|
45 |
+
x_prompt = gr.Text(
|
46 |
+
label="X Prompt",
|
47 |
+
show_label=False,
|
48 |
+
container=False,)
|
49 |
+
|
50 |
+
|
51 |
+
with gr.Tab("GenPicPrompt"):
|
52 |
+
|
53 |
+
input_img = gr.Image(
|
54 |
+
label="Input Picture",
|
55 |
+
show_label=False,
|
56 |
+
height=320,
|
57 |
+
type="filepath",
|
58 |
+
)
|
59 |
+
|
60 |
+
florence_btn = gr.Button(value="GenPrompt")
|
61 |
+
|
62 |
+
output_text = gr.Textbox(
|
63 |
+
label="Output Text", show_label=False, container=False
|
64 |
+
)
|
65 |
+
|
66 |
+
|
67 |
+
florence_btn.click(
|
68 |
+
fn=feifeiflorence, # Function to run when the button is clicked
|
69 |
+
inputs=[input_img], # Input components for the function
|
70 |
+
outputs=[output_text], # Output component for the function
|
71 |
+
)
|
72 |
+
|
73 |
+
feifei_chat_btn.click(
|
74 |
+
fn=feifeichat,
|
75 |
+
inputs=[feifei_chat_text1, feifei_chat_text2, feifei_chat_Dropdown],
|
76 |
+
outputs=[prompt, x_prompt]
|
77 |
+
)
|
78 |
+
return FeiFei
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mistralai
|
2 |
+
requests
|
3 |
+
opencv-python
|
4 |
+
opencv-python-headless
|
5 |
+
pillow
|
6 |
+
openai
|