Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import requests
|
4 |
+
from deep_translator import GoogleTranslator
|
5 |
+
from langdetect import detect
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
def get_random_api_key():
|
10 |
+
keys = os.getenv("KEYS", "").split(",")
|
11 |
+
if keys and keys[0]: # Check if KEYS is set and not empty
|
12 |
+
return random.choice(keys).strip()
|
13 |
+
else:
|
14 |
+
raise ValueError("API keys not found. Please set the KEYS environment variable.")
|
15 |
+
|
16 |
+
# Ссылка на файл CSS
|
17 |
+
css_url = "https://neurixyufi-aihub.static.hf.space/style.css"
|
18 |
+
|
19 |
+
# Получение CSS по ссылке
|
20 |
+
try:
|
21 |
+
response = requests.get(css_url)
|
22 |
+
response.raise_for_status()
|
23 |
+
css = response.text + " h1{text-align:center} .container { max-width: 800px; margin: 0 auto; }"
|
24 |
+
except requests.exceptions.RequestException as e:
|
25 |
+
print(f"Ошибка при загрузке CSS: {e}")
|
26 |
+
css = " h1{text-align:center} .container { max-width: 800px; margin: 0 auto; }"
|
27 |
+
|
28 |
+
|
29 |
+
def generate_story(prompt, style, api_key):
|
30 |
+
try:
|
31 |
+
client = InferenceClient(api_key=api_key, model_id="Qwen/QwQ-32B-Preview")
|
32 |
+
|
33 |
+
language = detect(prompt)
|
34 |
+
if language != 'en':
|
35 |
+
prompt = GoogleTranslator(source=language, target='en').translate(prompt)
|
36 |
+
|
37 |
+
messages = [
|
38 |
+
{"role": "system", "content": f"Напиши историю в стиле {style}."},
|
39 |
+
{"role": "user", "content": prompt}
|
40 |
+
]
|
41 |
+
|
42 |
+
completion = client.chat.completions.create(messages=messages, max_tokens=1000)
|
43 |
+
story = completion.choices[0].message.content
|
44 |
+
return story
|
45 |
+
except Exception as e:
|
46 |
+
return f"Ошибка генерации: {e}"
|
47 |
+
|
48 |
+
|
49 |
+
def edit_story(original_story, edited_prompt, api_key):
|
50 |
+
try:
|
51 |
+
client = InferenceClient(api_key=api_key, model_id="Qwen/QwQ-32B-Preview")
|
52 |
+
|
53 |
+
messages = [
|
54 |
+
{"role": "system", "content": "Отредактируй историю, учитывая предоставленные указания."},
|
55 |
+
{"role": "user", "content": edited_prompt},
|
56 |
+
{"role": "assistant", "content": original_story}
|
57 |
+
]
|
58 |
+
|
59 |
+
completion = client.chat.completions.create(messages=messages, max_tokens=1000)
|
60 |
+
edited_story = completion.choices[0].message.content
|
61 |
+
return edited_story
|
62 |
+
except Exception as e:
|
63 |
+
return f"Ошибка редактирования: {e}"
|
64 |
+
|
65 |
+
with gr.Blocks(css=css) as demo:
|
66 |
+
with gr.Row():
|
67 |
+
style_choices = ["Приключенческая", "Научно-фантастическая", "Романтическая", "Комедийная", "Трагическая", "Введите свой стиль:"]
|
68 |
+
style = gr.Dropdown(choices=style_choices, label="Выберите стиль истории", value="Приключенческая")
|
69 |
+
with gr.Row():
|
70 |
+
prompt = gr.Textbox(label="Введите запрос для истории", placeholder="Например: История о путешествии в космос", lines=5)
|
71 |
+
with gr.Row():
|
72 |
+
generate_button = gr.Button("Сгенерировать историю", variant='primary')
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
output_story = gr.Textbox(label="История", lines=10)
|
76 |
+
|
77 |
+
with gr.Tab("Редактирование"):
|
78 |
+
edited_prompt = gr.Textbox(label="Введите изменения для истории", placeholder="Например: Сделай историю более захватывающей", lines=5)
|
79 |
+
edited_story = gr.Textbox(label="Отредактированная история", lines=10)
|
80 |
+
edit_button = gr.Button("Отредактировать", variant='primary')
|
81 |
+
|
82 |
+
api_key = get_random_api_key()
|
83 |
+
generate_button.click(generate_story, inputs=[prompt, style], outputs=[output_story], api_key=api_key)
|
84 |
+
edit_button.click(edit_story, inputs=[output_story, edited_prompt], outputs=[edited_story], api_key=api_key)
|
85 |
+
|
86 |
+
demo.launch(show_api=False, share=False,concurrency_limit=250)
|