Spaces:
Sleeping
Sleeping
start api
Browse files- app.py +39 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# pipeline as high level
|
| 5 |
+
pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct", device="cpu")
|
| 6 |
+
|
| 7 |
+
# 3 adjectives + don't repeat + don't elaborate
|
| 8 |
+
system_instruction = """
|
| 9 |
+
You are given a short description of a person.
|
| 10 |
+
Respond only with three different adjectives that best describe the person, based on the description.
|
| 11 |
+
No explanation.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def get_vibe(caption):
|
| 15 |
+
messages = [
|
| 16 |
+
{"role": "system", "content": system_instruction},
|
| 17 |
+
{"role": "user", "content": caption}
|
| 18 |
+
]
|
| 19 |
+
output = pipe(messages, max_new_tokens=32, do_sample=True, temperature=0.8, top_p=0.9)[0]["generated_text"]
|
| 20 |
+
|
| 21 |
+
# extract assistant reply
|
| 22 |
+
assistant_text = next(
|
| 23 |
+
(entry['content'] for entry in output if entry['role'] == 'assistant'),
|
| 24 |
+
""
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# normalize to "word, word, word"
|
| 28 |
+
cleaned = assistant_text.replace(", and", ",").replace(" and", ",")
|
| 29 |
+
adjectives = [word.strip() for word in cleaned.split(",") if word.strip()]
|
| 30 |
+
return ", ".join(adjectives[:3])
|
| 31 |
+
|
| 32 |
+
# api w/ gradio
|
| 33 |
+
api = gr.Interface(
|
| 34 |
+
fn=get_vibe,
|
| 35 |
+
inputs=gr.Textbox(label="Input Caption"),
|
| 36 |
+
outputs="text"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
api.launch(show_api=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|