Update app.py
Browse files
app.py
CHANGED
|
@@ -25,41 +25,41 @@ llama = Llama(
|
|
| 25 |
|
| 26 |
|
| 27 |
# Function to generate responses
|
| 28 |
-
def generate_response(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
|
| 65 |
# JavaScript function for `on_load`
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# Function to generate responses
|
| 28 |
+
# def generate_response(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
|
| 29 |
+
# # chat_prompt = f"You are an Urdu Chatbot. Write an appropriate response for the given instruction: {message} Response:"
|
| 30 |
+
# chat_prompt = f"{system_prompt}\n ### Instruction: {message}\n ### Response:"
|
| 31 |
+
# response = llama(chat_prompt, temperature=temperature, max_tokens=max_new_tokens, top_k=top_k, repeat_penalty=repetition_penalty, top_p=top_p, stop=["Q:", "\n"], echo=False, stream=True)
|
| 32 |
|
| 33 |
+
# text = ""
|
| 34 |
+
# for chunk in response:
|
| 35 |
+
# content = chunk["choices"][0]["text"]
|
| 36 |
+
# if content:
|
| 37 |
+
# text += content
|
| 38 |
+
# yield text
|
| 39 |
|
| 40 |
+
def generate_response(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
|
| 41 |
+
"""Generates a streaming response from the Llama model."""
|
| 42 |
+
messages = [
|
| 43 |
+
{"role": "system", "content": "You are an Urdu Chatbot. Write an appropriate response for the given instruction."},
|
| 44 |
+
]
|
| 45 |
|
| 46 |
+
# Add history and the current message
|
| 47 |
+
for user, bot in history:
|
| 48 |
+
messages.append({"role": "user", "content": user})
|
| 49 |
+
messages.append({"role": "assistant", "content": bot})
|
| 50 |
|
| 51 |
+
messages.append({"role": "user", "content": message})
|
| 52 |
|
| 53 |
+
response = llama.create_chat_completion(
|
| 54 |
+
messages=messages,
|
| 55 |
+
stream=True,
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
partial_message = ""
|
| 59 |
+
for part in response:
|
| 60 |
+
content = part["choices"][0]["delta"].get("content", "")
|
| 61 |
+
partial_message += content
|
| 62 |
+
yield partial_message
|
| 63 |
|
| 64 |
|
| 65 |
# JavaScript function for `on_load`
|