Rid3 commited on
Commit
ebb0d51
Β·
verified Β·
1 Parent(s): 4b46556

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ TEXT_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
5
+ IMAGE_MODEL = "black-forest-labs/FLUX.1-schnell"
6
+
7
+
8
+ # ── ЧАВ ──────────────────────────────────────────────────────────────────────
9
+ def respond(message, history, system_prompt, max_tokens, temperature, hf_token: gr.OAuthToken):
10
+ client = InferenceClient(token=hf_token.token)
11
+
12
+ messages = [{"role": "system", "content": system_prompt}]
13
+ for h in history:
14
+ messages.append({"role": h["role"], "content": h["content"]})
15
+ messages.append({"role": "user", "content": message})
16
+
17
+ response = ""
18
+ for chunk in client.chat_completion(
19
+ messages,
20
+ model=TEXT_MODEL,
21
+ max_tokens=max_tokens,
22
+ temperature=temperature,
23
+ stream=True,
24
+ ):
25
+ token = chunk.choices[0].delta.content or ""
26
+ response += token
27
+ yield response
28
+
29
+
30
+ # ── Π“Π•ΠΠ•Π ΠΠ¦Π˜Π― ЀОВО ────────────────────────────────────────────────────────────
31
+ def generate_image(prompt, steps, hf_token: gr.OAuthToken):
32
+ if not prompt.strip():
33
+ return None
34
+ client = InferenceClient(token=hf_token.token)
35
+ image = client.text_to_image(
36
+ prompt,
37
+ model=IMAGE_MODEL,
38
+ num_inference_steps=steps,
39
+ )
40
+ return image
41
+
42
+
43
+ # ── Π˜ΠΠ’Π•Π Π€Π•Π™Π‘ ─────────────────────────────────────────────────────────────────
44
+ css = """
45
+ footer { display: none !important; }
46
+ """
47
+
48
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), css=css, title="AI АссистСнт") as demo:
49
+
50
+ gr.Markdown("# πŸ€– AI АссистСнт")
51
+ gr.Markdown("Π’ΠΎΠΉΠ΄ΠΈ Ρ‡Π΅Ρ€Π΅Π· HuggingFace β€” это бСсплатно, ΠΊΠ»ΡŽΡ‡ΠΈ Π½Π΅ Π½ΡƒΠΆΠ½Ρ‹.")
52
+
53
+ with gr.Sidebar():
54
+ gr.LoginButton()
55
+ gr.Markdown("---")
56
+ gr.Markdown("**МодСли:**")
57
+ gr.Markdown(f"πŸ’¬ `{TEXT_MODEL}`")
58
+ gr.Markdown(f"🎨 `{IMAGE_MODEL}`")
59
+
60
+ with gr.Tabs():
61
+
62
+ # ── Π’ΠΊΠ»Π°Π΄ΠΊΠ°: Π§Π°Ρ‚ ──────────────────────────────────────────────────────
63
+ with gr.Tab("πŸ’¬ Π§Π°Ρ‚"):
64
+ gr.ChatInterface(
65
+ respond,
66
+ type="messages",
67
+ chatbot=gr.Chatbot(height=480, render_markdown=True, show_label=False),
68
+ textbox=gr.Textbox(placeholder="Напиши Ρ‡Ρ‚ΠΎ-Π½ΠΈΠ±ΡƒΠ΄ΡŒ...", container=False),
69
+ additional_inputs=[
70
+ gr.Textbox(
71
+ value="Π’Ρ‹ ΡƒΠΌΠ½Ρ‹ΠΉ ΠΈ Π΄Ρ€ΡƒΠΆΠ΅Π»ΡŽΠ±Π½Ρ‹ΠΉ ассистСнт. ΠžΡ‚Π²Π΅Ρ‡Π°ΠΉ Π½Π° языкС ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ.",
72
+ label="БистСмный ΠΏΡ€ΠΎΠΌΠΏΡ‚",
73
+ lines=2,
74
+ ),
75
+ gr.Slider(256, 2048, value=512, step=128, label="Макс. Ρ‚ΠΎΠΊΠ΅Π½ΠΎΠ²"),
76
+ gr.Slider(0.1, 1.5, value=0.7, step=0.05, label="Π’Π΅ΠΌΠΏΠ΅Ρ€Π°Ρ‚ΡƒΡ€Π°"),
77
+ ],
78
+ additional_inputs_accordion=gr.Accordion("βš™οΈ Настройки", open=False),
79
+ submit_btn="ΠžΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ➀",
80
+ retry_btn="πŸ”„",
81
+ undo_btn="↩️",
82
+ clear_btn="πŸ—‘οΈ",
83
+ )
84
+
85
+ # ── Π’ΠΊΠ»Π°Π΄ΠΊΠ°: ГСнСрация Ρ„ΠΎΡ‚ΠΎ ────────────────────────────────────────────
86
+ with gr.Tab("🎨 ГСнСрация Ρ„ΠΎΡ‚ΠΎ"):
87
+ with gr.Row():
88
+ with gr.Column(scale=2):
89
+ prompt_box = gr.Textbox(
90
+ label="ОписаниС ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ",
91
+ placeholder="Π·Π°ΠΊΠ°Ρ‚ Π½Π° ΠΌΠΎΡ€Π΅, Π°Π½ΠΈΠΌΠ΅ ΡΡ‚ΠΈΠ»ΡŒ, 4K...",
92
+ lines=4,
93
+ )
94
+ steps_slider = gr.Slider(1, 8, value=4, step=1, label="Π¨Π°Π³ΠΈ (быстрСС = мСньшС)")
95
+ gen_btn = gr.Button("🎨 Π‘ΠΎΠ·Π΄Π°Ρ‚ΡŒ", variant="primary", size="lg")
96
+
97
+ with gr.Column(scale=3):
98
+ output_image = gr.Image(label="Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚", show_label=False)
99
+
100
+ gen_btn.click(
101
+ fn=generate_image,
102
+ inputs=[prompt_box, steps_slider],
103
+ outputs=output_image,
104
+ )
105
+
106
+ gr.Examples(
107
+ examples=[
108
+ ["a futuristic cyberpunk city at night, neon lights, rain, 4K"],
109
+ ["cute anime girl in a flower field, soft light, detailed"],
110
+ ["astronaut riding a horse on Mars, cinematic, realistic"],
111
+ ["a cozy cabin in snowy forest, warm light inside, peaceful"],
112
+ ],
113
+ inputs=prompt_box,
114
+ )
115
+
116
+ if __name__ == "__main__":
117
+ demo.launch()