Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def process_event(text, system_message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
messages = [
|
| 8 |
{"role": "system", "content": system_message},
|
| 9 |
-
{"role": "user", "content":
|
| 10 |
]
|
| 11 |
|
| 12 |
-
# Получаем ответ от модели
|
| 13 |
-
response = client.chat_completion(
|
| 14 |
-
messages,
|
| 15 |
-
max_tokens=200,
|
| 16 |
-
temperature=0.1,
|
| 17 |
-
top_p=0.95,
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
# Правильно извлекаем текст из ответа
|
| 21 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
generated_text = response.choices[0].message.content
|
| 23 |
return generated_text
|
| 24 |
except Exception as e:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
calendar_system_message = """You are a calendar event parser assistant. Your task is to:
|
| 28 |
-
1. Understand the event description provided by the user
|
| 29 |
-
2. Always respond in the same language as the user's message
|
| 30 |
-
3. Fix any grammar mistakes in event names and descriptions
|
| 31 |
-
4. Return ONLY a valid JSON object with this exact format:
|
| 32 |
-
{
|
| 33 |
-
"event_name": "<corrected event name>",
|
| 34 |
-
"datefrom": "2025-01-DD HH:mm",
|
| 35 |
-
"dateto": "2025-01-DD HH:mm",
|
| 36 |
-
"description": "<improved event description>"
|
| 37 |
-
}
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
-
# Создаем
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=process_event,
|
| 43 |
inputs=[
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Системное сообщение с инструкциями
|
| 8 |
+
calendar_system_message = """You are a calendar event parser. Follow these rules STRICTLY:
|
| 9 |
+
|
| 10 |
+
LANGUAGE RULES:
|
| 11 |
+
- If input contains Cyrillic letters - respond in Russian ONLY
|
| 12 |
+
- If input is in English - respond in English ONLY
|
| 13 |
+
- Never mix languages in response
|
| 14 |
+
|
| 15 |
+
DATE AND TIME RULES:
|
| 16 |
+
- Today's date is 2025-01-02
|
| 17 |
+
- All events should be scheduled in the future relative to today
|
| 18 |
+
- If month is not specified, use next occurrence of that date
|
| 19 |
+
- If only day is mentioned (like "7th"), use next occurrence in January 2025
|
| 20 |
+
- Always use year 2025 for all dates
|
| 21 |
+
- Format times in 24-hour format (HH:mm)
|
| 22 |
+
|
| 23 |
+
FORMAT RULES:
|
| 24 |
+
Return exactly this JSON with no additional text:
|
| 25 |
+
{
|
| 26 |
+
"event_name": "<name in same language as input>",
|
| 27 |
+
"datefrom": "2025-01-DD HH:mm",
|
| 28 |
+
"dateto": "2025-01-DD HH:mm",
|
| 29 |
+
"description": "<description in same language as input>"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
RUSSIAN INPUT EXAMPLE:
|
| 33 |
+
Input: "встреча 7 числа в 15:00"
|
| 34 |
+
Response:
|
| 35 |
+
{
|
| 36 |
+
"event_name": "Встреча",
|
| 37 |
+
"datefrom": "2025-01-07 15:00",
|
| 38 |
+
"dateto": "2025-01-07 16:00",
|
| 39 |
+
"description": "Деловая встреча 7 января"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
EXTRA RULES:
|
| 43 |
+
- Keep all text fields in the EXACT same language as input
|
| 44 |
+
- Fix grammar and spelling while keeping original language
|
| 45 |
+
- NEVER translate event names or descriptions to another language
|
| 46 |
+
- Make descriptions clear and professional in the original language
|
| 47 |
+
- Always assume current year is 2025"""
|
| 48 |
+
|
| 49 |
+
def detect_language(text):
|
| 50 |
+
# Проверяем наличие кириллицы
|
| 51 |
+
has_cyrillic = bool(re.search('[а-яА-Я]', text))
|
| 52 |
+
return 'ru' if has_cyrillic else 'en'
|
| 53 |
+
|
| 54 |
def process_event(text, system_message):
|
| 55 |
+
lang = detect_language(text)
|
| 56 |
+
|
| 57 |
+
# Добавляем явное указание языка в промпт
|
| 58 |
+
if lang == 'ru':
|
| 59 |
+
prompt = f"Ответ должен быть на русском языке. Текст события: {text}"
|
| 60 |
+
else:
|
| 61 |
+
prompt = f"Response must be in English. Event text: {text}"
|
| 62 |
+
|
| 63 |
messages = [
|
| 64 |
{"role": "system", "content": system_message},
|
| 65 |
+
{"role": "user", "content": prompt}
|
| 66 |
]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
+
response = client.chat_completion(
|
| 70 |
+
messages,
|
| 71 |
+
max_tokens=200,
|
| 72 |
+
temperature=0.1,
|
| 73 |
+
top_p=0.95,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
generated_text = response.choices[0].message.content
|
| 77 |
return generated_text
|
| 78 |
except Exception as e:
|
| 79 |
+
error_msg = "Ошибка обработки" if lang == 'ru' else "Processing error"
|
| 80 |
+
return f"{error_msg}: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Создаем интерфейс
|
| 83 |
demo = gr.Interface(
|
| 84 |
fn=process_event,
|
| 85 |
inputs=[
|