Update app.py
Browse files
app.py
CHANGED
@@ -13,44 +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
|
20 |
-
#
|
21 |
-
text = text.replace("ASSISTANT:", "").
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
for
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
words = text.split()
|
30 |
-
text = ' '.join(words)
|
31 |
-
|
32 |
-
return text
|
33 |
|
34 |
def chat(message, history):
|
35 |
-
system_prompt = """Ti si YugoGPT
|
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 |
|
47 |
full_prompt = f"""SYSTEM: {system_prompt}
|
48 |
|
49 |
-
KONTEKST:
|
50 |
{chat_history}
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
response = llm(
|
56 |
full_prompt,
|
@@ -59,25 +49,25 @@ Odgovor:"""
|
|
59 |
top_p=0.1,
|
60 |
repeat_penalty=1.2,
|
61 |
top_k=20,
|
62 |
-
stop=["
|
63 |
stream=True
|
64 |
)
|
65 |
|
66 |
partial_message = ""
|
67 |
for chunk in response:
|
68 |
if chunk and chunk['choices'][0]['text']:
|
69 |
-
text =
|
70 |
-
partial_message
|
71 |
yield partial_message
|
72 |
|
73 |
demo = gr.ChatInterface(
|
74 |
fn=chat,
|
75 |
-
title="YugoGPT
|
76 |
-
description="
|
77 |
examples=[
|
78 |
-
"
|
79 |
-
"
|
80 |
-
"
|
81 |
]
|
82 |
)
|
83 |
|
@@ -88,3 +78,4 @@ if __name__ == "__main__":
|
|
88 |
share=False
|
89 |
)
|
90 |
|
|
|
|
13 |
def format_chat_history(history):
|
14 |
formatted_history = ""
|
15 |
for user_msg, assistant_msg in history:
|
16 |
+
formatted_history += f"Q: {user_msg}\nA: {assistant_msg}\n"
|
17 |
return formatted_history
|
18 |
|
19 |
+
def process_chunk(text):
|
20 |
+
# Remove any special tokens
|
21 |
+
text = text.replace("ASSISTANT:", "").replace("A:", "")
|
22 |
|
23 |
+
# Add space after each character if there isn't one already
|
24 |
+
processed_text = ""
|
25 |
+
for i, char in enumerate(text):
|
26 |
+
processed_text += char
|
27 |
+
if i < len(text) - 1 and not text[i+1].isspace():
|
28 |
+
processed_text += " "
|
29 |
|
30 |
+
return processed_text.strip()
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def chat(message, history):
|
33 |
+
system_prompt = """Ti si YugoGPT.
|
34 |
+
Odgovaraj direktno i precizno na srpskom jeziku."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
chat_history = format_chat_history(history)
|
37 |
|
38 |
full_prompt = f"""SYSTEM: {system_prompt}
|
39 |
|
|
|
40 |
{chat_history}
|
41 |
|
42 |
+
Q: {message}
|
43 |
+
A:"""
|
44 |
|
45 |
response = llm(
|
46 |
full_prompt,
|
|
|
49 |
top_p=0.1,
|
50 |
repeat_penalty=1.2,
|
51 |
top_k=20,
|
52 |
+
stop=["Q:", "\n\n"],
|
53 |
stream=True
|
54 |
)
|
55 |
|
56 |
partial_message = ""
|
57 |
for chunk in response:
|
58 |
if chunk and chunk['choices'][0]['text']:
|
59 |
+
text = process_chunk(chunk['choices'][0]['text'])
|
60 |
+
partial_message += text
|
61 |
yield partial_message
|
62 |
|
63 |
demo = gr.ChatInterface(
|
64 |
fn=chat,
|
65 |
+
title="YugoGPT",
|
66 |
+
description="Stručni asistent",
|
67 |
examples=[
|
68 |
+
"Objasni kako radi HTTP protokol",
|
69 |
+
"Šta je SQL?",
|
70 |
+
"Kako radi TCP/IP?"
|
71 |
]
|
72 |
)
|
73 |
|
|
|
78 |
share=False
|
79 |
)
|
80 |
|
81 |
+
|