Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,47 +4,29 @@ import re
|
|
| 4 |
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
calendar_system_message =
|
| 8 |
|
| 9 |
-
|
| 10 |
-
- If user input contains ANY Cyrillic letters - you MUST respond in Russian language
|
| 11 |
-
- If user input is in English - respond in English
|
| 12 |
-
- NEVER mix languages
|
| 13 |
-
- NEVER translate to another language
|
| 14 |
-
- The language of the input DICTATES the language of the output
|
| 15 |
-
|
| 16 |
-
DATE AND TIME RULES:
|
| 17 |
-
- Current date: 2025-01-02
|
| 18 |
-
- All events MUST be in 2025
|
| 19 |
-
- If day number is less than today's date, use the next suitable month
|
| 20 |
-
- Use 24-hour time format (HH:mm)
|
| 21 |
-
|
| 22 |
-
FORMAT:
|
| 23 |
-
For Russian input, return ONLY:
|
| 24 |
{
|
| 25 |
-
"event_name": "
|
| 26 |
"datefrom": "2025-01-DD HH:mm",
|
| 27 |
"dateto": "2025-01-DD HH:mm",
|
| 28 |
-
"description": "
|
| 29 |
}
|
| 30 |
|
| 31 |
-
|
| 32 |
{
|
| 33 |
-
"event_name": "
|
| 34 |
"datefrom": "2025-01-DD HH:mm",
|
| 35 |
"dateto": "2025-01-DD HH:mm",
|
| 36 |
-
"description": "
|
| 37 |
}
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"datefrom": "2025-01-07 15:00",
|
| 45 |
-
"dateto": "2025-01-07 16:00",
|
| 46 |
-
"description": "Плановая встреча 7 января"
|
| 47 |
-
}"""
|
| 48 |
|
| 49 |
def detect_language(text):
|
| 50 |
has_cyrillic = bool(re.search('[а-яА-Я]', text))
|
|
@@ -54,11 +36,11 @@ def process_event(text, system_message):
|
|
| 54 |
lang = detect_language(text)
|
| 55 |
|
| 56 |
if lang == 'ru':
|
| 57 |
-
prompt = f"""
|
| 58 |
-
|
| 59 |
-
Текст события: {text}"""
|
| 60 |
else:
|
| 61 |
-
prompt = f"
|
|
|
|
| 62 |
|
| 63 |
messages = [
|
| 64 |
{"role": "system", "content": system_message},
|
|
@@ -71,11 +53,14 @@ def process_event(text, system_message):
|
|
| 71 |
max_tokens=200,
|
| 72 |
temperature=0.1,
|
| 73 |
top_p=0.95,
|
| 74 |
-
presence_penalty=0,
|
| 75 |
-
frequency_penalty=0
|
| 76 |
)
|
| 77 |
|
| 78 |
generated_text = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return generated_text
|
| 80 |
except Exception as e:
|
| 81 |
error_msg = "Ошибка обработки" if lang == 'ru' else "Processing error"
|
|
|
|
| 4 |
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
calendar_system_message = '''You are a simple calendar parser.
|
| 8 |
|
| 9 |
+
If input contains Russian text, respond with ONLY this JSON in Russian:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
{
|
| 11 |
+
"event_name": "название",
|
| 12 |
"datefrom": "2025-01-DD HH:mm",
|
| 13 |
"dateto": "2025-01-DD HH:mm",
|
| 14 |
+
"description": "описание"
|
| 15 |
}
|
| 16 |
|
| 17 |
+
If input is English, respond with ONLY this JSON in English:
|
| 18 |
{
|
| 19 |
+
"event_name": "name",
|
| 20 |
"datefrom": "2025-01-DD HH:mm",
|
| 21 |
"dateto": "2025-01-DD HH:mm",
|
| 22 |
+
"description": "description"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
Rules:
|
| 26 |
+
1. Return ONLY the JSON object, nothing else
|
| 27 |
+
2. Current date is 2025-01-02
|
| 28 |
+
3. Use 24-hour time format
|
| 29 |
+
4. Keep response in same language as input'''
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def detect_language(text):
|
| 32 |
has_cyrillic = bool(re.search('[а-яА-Я]', text))
|
|
|
|
| 36 |
lang = detect_language(text)
|
| 37 |
|
| 38 |
if lang == 'ru':
|
| 39 |
+
prompt = f"""ТОЛЬКО JSON на русском языке, без дополнительного текста:
|
| 40 |
+
Событие: {text}"""
|
|
|
|
| 41 |
else:
|
| 42 |
+
prompt = f"""ONLY JSON in English, no additional text:
|
| 43 |
+
Event: {text}"""
|
| 44 |
|
| 45 |
messages = [
|
| 46 |
{"role": "system", "content": system_message},
|
|
|
|
| 53 |
max_tokens=200,
|
| 54 |
temperature=0.1,
|
| 55 |
top_p=0.95,
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
generated_text = response.choices[0].message.content
|
| 59 |
+
# Пытаемся извлечь только JSON часть ответа
|
| 60 |
+
json_start = generated_text.find('{')
|
| 61 |
+
json_end = generated_text.rfind('}') + 1
|
| 62 |
+
if json_start >= 0 and json_end > json_start:
|
| 63 |
+
return generated_text[json_start:json_end]
|
| 64 |
return generated_text
|
| 65 |
except Exception as e:
|
| 66 |
error_msg = "Ошибка обработки" if lang == 'ru' else "Processing error"
|