File size: 1,459 Bytes
1915306 c02b6a1 1915306 8b5f3bd db29e59 8b5f3bd 4a65c44 8b5f3bd 1915306 8b5f3bd e8b0ec8 1915306 0c08de9 987b836 1915306 987b836 1915306 0c08de9 1915306 8b5f3bd 1915306 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("Qwen/Qwen1.5-4B-Chat")
def respond(message, history: list[tuple[str, str]]):
system_message = "You are a friendly Chatbot. Respond only in bisaya language. No english translation."
max_tokens = 4096
temperature = 0.6
top_p = 0.95
messages = [{"role": "system", "content": system_message}]
for user_text, assistant_text in history:
if user_text:
messages.append({"role": "user", "content": user_text})
if assistant_text:
messages.append({"role": "assistant", "content": assistant_text})
messages.append({"role": "user", "content": message})
response = ""
previous_response = ""
for token_message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = token_message.choices[0].delta.get("content", "")
if not token:
break
response += token
# Only yield if new content was added
if response != previous_response:
yield response
previous_response = response
# Optional: break out if the response is too long to avoid infinite loops
if len(response) > 3000: # adjust threshold as needed
break
demo = gr.ChatInterface(respond)
if __name__ == "__main__":
demo.launch()
|