|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
MERGED_MODEL_REPO = "Grandediw/lora-model_finetuned" |
|
|
|
|
|
client = InferenceClient(MERGED_MODEL_REPO) |
|
|
|
|
|
|
|
def mini_chatbot(message, history): |
|
""" |
|
This function simulates a conversation with the model. It takes the latest user message |
|
and the full conversation history, builds a prompt, and returns the model's response. |
|
""" |
|
|
|
|
|
system_message = "You are a helpful and friendly assistant." |
|
|
|
|
|
|
|
|
|
prompt = system_message.strip() + "\n\n" |
|
for user_msg, assistant_msg in history: |
|
if user_msg: |
|
prompt += f"User: {user_msg}\n" |
|
if assistant_msg: |
|
prompt += f"Assistant: {assistant_msg}\n" |
|
prompt += f"User: {message}\nAssistant:" |
|
|
|
|
|
max_tokens = 200 |
|
temperature = 0.7 |
|
top_p = 0.9 |
|
|
|
|
|
response = "" |
|
for partial in client.text_generation( |
|
prompt=prompt, |
|
max_new_tokens=max_tokens, |
|
temperature=temperature, |
|
top_p=top_p, |
|
stream=True |
|
): |
|
token = partial.token.text |
|
response += token |
|
|
|
return response |
|
|
|
|
|
|
|
demo_chatbot = gr.ChatInterface( |
|
fn=mini_chatbot, |
|
title="My Chatbot", |
|
description="Enter text to start chatting with the merged LoRA model." |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo_chatbot.launch() |
|
|