Ashrafb commited on
Commit
ee4e262
·
verified ·
1 Parent(s): 4314b29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -35,12 +35,13 @@ def get_prompt(message: str, chat_history: list[tuple[str, str]],
35
 
36
 
37
  def run(message: str,
38
- chat_history: list[tuple[str, str]],
39
- system_prompt: str,
40
- max_new_tokens: int = 1024,
41
- temperature: float = 0.1,
42
- top_p: float = 0.9,
43
- top_k: int = 50) -> Iterator[str]:
 
44
  prompt = get_prompt(message, chat_history, system_prompt)
45
 
46
  generate_kwargs = dict(
@@ -50,23 +51,17 @@ def run(message: str,
50
  top_k=top_k,
51
  temperature=temperature,
52
  )
53
- -> Iterator[list[tuple[str, str]]]:
54
- if max_new_tokens > MAX_MAX_NEW_TOKENS:
55
- raise ValueError
56
 
57
- history = history_with_input[:-1]
58
- generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
59
-
60
- try:
61
- first_response = next(generator)
62
- translated_response = translator_to_ar.translate(first_response) # Translate the response to Arabic
63
- yield history + [(message, translated_response)]
64
- except StopIteration:
65
- yield history + [(message, '')]
66
 
67
- for response in generator:
68
- translated_response = translator_to_ar.translate(response) # Translate each response to Arabic
69
- yield history + [(message, translated_response)]
 
 
 
 
70
 
71
 
72
  def generate_image_caption(image_data):
 
35
 
36
 
37
  def run(message: str,
38
+ chat_history: list[tuple[str, str]],
39
+ system_prompt: str,
40
+ max_new_tokens: int = 1024,
41
+ temperature: float = 0.1,
42
+ top_p: float = 0.9,
43
+ top_k: int = 50) -> Iterator[str]:
44
+
45
  prompt = get_prompt(message, chat_history, system_prompt)
46
 
47
  generate_kwargs = dict(
 
51
  top_k=top_k,
52
  temperature=temperature,
53
  )
 
 
 
54
 
55
+ stream = client.generate_stream(prompt, **generate_kwargs)
56
+ output = ""
 
 
 
 
 
 
 
57
 
58
+ for response in stream:
59
+ if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
60
+ translated_output = translator_to_ar.translate(output)
61
+ yield translated_output
62
+ output = ""
63
+ else:
64
+ output += response.token.text
65
 
66
 
67
  def generate_image_caption(image_data):