cigol123 commited on
Commit
204eabb
·
verified ·
1 Parent(s): 602b78e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -33
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"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
 
47
  full_prompt = f"""SYSTEM: {system_prompt}
48
 
49
- KONTEKST:
50
  {chat_history}
51
 
52
- Pitanje: {message}
53
- Odgovor:"""
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=["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
 
@@ -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
+