Spaces:
Sleeping
Sleeping
feat: init app
Browse files- .gitignore +2 -0
- app.py +77 -60
- prompts.json +18 -0
- requirements.txt +4 -1
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.aider*
|
2 |
+
.env
|
app.py
CHANGED
@@ -1,64 +1,81 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Configuration
|
8 |
+
MODEL_NAME = "speakleash/Bielik-11B-v2.3-Instruct"
|
9 |
+
# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
DEVICE = "cuda"
|
11 |
+
TORCH_DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
12 |
+
MAX_TOKENS = 1000
|
13 |
+
|
14 |
+
# Load model and tokenizer
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=TORCH_DTYPE).to(
|
17 |
+
DEVICE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
|
20 |
+
# Load prompts
|
21 |
+
with open("prompts.json") as f:
|
22 |
+
prompts = json.load(f)
|
23 |
+
|
24 |
+
|
25 |
+
@spaces.GPU
|
26 |
+
def transform_text(prompt_name, user_input):
|
27 |
+
"""Transform text using selected prompt and Bielik model"""
|
28 |
+
try:
|
29 |
+
# Get selected prompt
|
30 |
+
selected_prompt = next(p for p in prompts if p["name"] == prompt_name)
|
31 |
+
|
32 |
+
# Create messages structure
|
33 |
+
messages = [
|
34 |
+
{"role": "system", "content": selected_prompt["system_message"]},
|
35 |
+
{"role": "user", "content": user_input},
|
36 |
+
]
|
37 |
+
|
38 |
+
# Tokenize and generate
|
39 |
+
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(
|
40 |
+
DEVICE
|
41 |
+
)
|
42 |
+
|
43 |
+
generated_ids = model.generate(
|
44 |
+
input_ids, max_new_tokens=MAX_TOKENS, do_sample=True
|
45 |
+
)
|
46 |
+
|
47 |
+
return tokenizer.batch_decode(generated_ids)[0]
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
return f"Error: {str(e)}"
|
51 |
+
|
52 |
+
|
53 |
+
# Create Gradio interface
|
54 |
+
with gr.Blocks(title="Bielik Goblin") as interface:
|
55 |
+
gr.Markdown("# Bielik Goblin")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
prompt_select = gr.Dropdown(
|
59 |
+
choices=[p["name"] for p in prompts],
|
60 |
+
label="Wybierz prompt",
|
61 |
+
interactive=True,
|
62 |
+
)
|
63 |
+
|
64 |
+
user_input = gr.Textbox(
|
65 |
+
label="Tw贸j tekst", placeholder="Wpisz tutaj sw贸j tekst...", lines=5
|
66 |
+
)
|
67 |
+
|
68 |
+
transform_btn = gr.Button("Przekszta艂膰 tekst", variant="primary")
|
69 |
+
|
70 |
+
with gr.Column():
|
71 |
+
progress = gr.StatusTracker()
|
72 |
+
output = gr.Textbox(label="Wynik", interactive=False)
|
73 |
+
|
74 |
+
transform_btn.click(
|
75 |
+
fn=transform_text,
|
76 |
+
inputs=[prompt_select, user_input],
|
77 |
+
outputs=output,
|
78 |
+
status=progress,
|
79 |
+
)
|
80 |
|
81 |
+
interface.queue().launch(debug=True)
|
|
prompts.json
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"name": "Parafraza",
|
4 |
+
"system_message": "Parafrazuj podany tekst zachowuj膮c jego g艂贸wn膮 ide臋. U偶ywaj naturalnego, potocznego j臋zyka polskiego."
|
5 |
+
},
|
6 |
+
{
|
7 |
+
"name": "Formalizacja",
|
8 |
+
"system_message": "Przekszta艂膰 tekst na formalny styl biznesowy. Zachowaj wszystkie kluczowe informacje."
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"name": "Korekta",
|
12 |
+
"system_message": "Popraw gramatyk臋, interpunkcj臋 i styl tekstu. Zachowaj oryginalne znaczenie."
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"name": "Podsumowanie",
|
16 |
+
"system_message": "Stw贸rz zwi臋z艂e podsumowanie tekstu, zachowuj膮c kluczowe punkty. Maksymalnie 3 zdania."
|
17 |
+
}
|
18 |
+
]
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0
|
2 |
+
transformers>=4.0
|
3 |
+
torch>=2.0
|
4 |
+
accelerate>=0.0
|