Update app.py
Browse files
app.py
CHANGED
@@ -13,20 +13,34 @@ llm = Llama(
|
|
13 |
def format_chat_history(history):
|
14 |
formatted_history = ""
|
15 |
for user_msg, assistant_msg in history:
|
16 |
-
formatted_history += f"
|
17 |
return formatted_history
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def chat(message, history):
|
20 |
-
system_prompt = """Ti si YugoGPT,
|
21 |
|
22 |
-
|
23 |
-
- Dajem
|
24 |
-
- Odgovaram
|
25 |
-
- Koristim jasan i precizan jezik
|
26 |
-
-
|
27 |
- Odgovaram direktno i pozitivno
|
28 |
-
-
|
29 |
-
- Uvek koristim pravilnu interpunkciju i razmake između reči"""
|
30 |
|
31 |
chat_history = format_chat_history(history)
|
32 |
|
@@ -35,8 +49,8 @@ PRAVILA RADA:
|
|
35 |
KONTEKST:
|
36 |
{chat_history}
|
37 |
|
38 |
-
|
39 |
-
"""
|
40 |
|
41 |
response = llm(
|
42 |
full_prompt,
|
@@ -45,31 +59,25 @@ USER: {message}
|
|
45 |
top_p=0.1,
|
46 |
repeat_penalty=1.2,
|
47 |
top_k=20,
|
48 |
-
stop=["
|
49 |
stream=True
|
50 |
)
|
51 |
|
52 |
partial_message = ""
|
53 |
for chunk in response:
|
54 |
if chunk and chunk['choices'][0]['text']:
|
55 |
-
text = chunk['choices'][0]['text']
|
56 |
-
|
57 |
-
|
58 |
-
# Ensure proper spacing after punctuation
|
59 |
-
text = text.replace(".", ". ").replace(",", ", ").replace("!", "! ").replace("?", "? ")
|
60 |
-
# Remove double spaces
|
61 |
-
text = " ".join(text.split())
|
62 |
-
partial_message += text
|
63 |
-
yield partial_message.strip()
|
64 |
|
65 |
demo = gr.ChatInterface(
|
66 |
fn=chat,
|
67 |
title="YugoGPT Stručni Asistent",
|
68 |
-
description="Precizan izvor informacija
|
69 |
examples=[
|
70 |
-
"
|
71 |
-
"
|
72 |
-
"
|
73 |
]
|
74 |
)
|
75 |
|
@@ -79,3 +87,4 @@ if __name__ == "__main__":
|
|
79 |
server_port=7860,
|
80 |
share=False
|
81 |
)
|
|
|
|
13 |
def format_chat_history(history):
|
14 |
formatted_history = ""
|
15 |
for user_msg, assistant_msg in history:
|
16 |
+
formatted_history += f"Pitanje: {user_msg}\nOdgovor: {assistant_msg}\n"
|
17 |
return formatted_history
|
18 |
|
19 |
+
def clean_text(text):
|
20 |
+
# Basic cleaning
|
21 |
+
text = text.replace("ASSISTANT:", "").strip()
|
22 |
+
|
23 |
+
# Handle spaces after punctuation
|
24 |
+
punctuation_marks = ['.', ',', '!', '?', ':', ';']
|
25 |
+
for mark in punctuation_marks:
|
26 |
+
text = text.replace(mark, mark + ' ')
|
27 |
+
|
28 |
+
# Normalize spaces
|
29 |
+
words = text.split()
|
30 |
+
text = ' '.join(words)
|
31 |
+
|
32 |
+
return text
|
33 |
+
|
34 |
def chat(message, history):
|
35 |
+
system_prompt = """Ti si YugoGPT, visoko precizan AI asistent.
|
36 |
|
37 |
+
OSNOVNI PRINCIPI:
|
38 |
+
- Dajem konkretne i tačne informacije
|
39 |
+
- Odgovaram samo o temama koje dobro poznajem
|
40 |
+
- Koristim jasan i precizan srpski jezik
|
41 |
+
- Fokusiram se na činjenice
|
42 |
- Odgovaram direktno i pozitivno
|
43 |
+
- Izbegavam nagađanja"""
|
|
|
44 |
|
45 |
chat_history = format_chat_history(history)
|
46 |
|
|
|
49 |
KONTEKST:
|
50 |
{chat_history}
|
51 |
|
52 |
+
Pitanje: {message}
|
53 |
+
Odgovor:"""
|
54 |
|
55 |
response = llm(
|
56 |
full_prompt,
|
|
|
59 |
top_p=0.1,
|
60 |
repeat_penalty=1.2,
|
61 |
top_k=20,
|
62 |
+
stop=["Pitanje:", "\n\n"],
|
63 |
stream=True
|
64 |
)
|
65 |
|
66 |
partial_message = ""
|
67 |
for chunk in response:
|
68 |
if chunk and chunk['choices'][0]['text']:
|
69 |
+
text = clean_text(chunk['choices'][0]['text'])
|
70 |
+
partial_message = clean_text(partial_message + text)
|
71 |
+
yield partial_message
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
demo = gr.ChatInterface(
|
74 |
fn=chat,
|
75 |
title="YugoGPT Stručni Asistent",
|
76 |
+
description="Precizan izvor informacija. PAŽNJA!!! ZNA DA LAŽE!!!",
|
77 |
examples=[
|
78 |
+
"Objasnite princip rada relacione baze podataka",
|
79 |
+
"Kako funkcioniše HTTP protokol?",
|
80 |
+
"Opišite osnovne komponente računara"
|
81 |
]
|
82 |
)
|
83 |
|
|
|
87 |
server_port=7860,
|
88 |
share=False
|
89 |
)
|
90 |
+
|