b-AI / app.py
ianeksdi's picture
Update app.py
c02b6a1 verified
raw
history blame
1.46 kB
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()