import gradio as gr from transformers import pipeline, AutoTokenizer,…
- User
import gradio as gr from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
Load an open-source LLM (e.g., BioGPT)
model_name = "microsoft/biogpt" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name)
Create a text generation pipeline
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
Define the chatbot logic
def respond(symptom_input, history: list[tuple[str, str]]): # Keep the system message only for internal logic, not to feed it to the model system_message = "You are a healthcare assistant chatbot. Provide medical advice and suggestions based on the user's symptoms." max_tokens = 512 # Hardcoded value for token limit temperature = 0.7 # Hardcoded value for temperature top_p = 0.95 # Hardcoded value for top-p sampling
# Build the conversation history without sending system messages to the model prompt = "" for val in history: if val[0]: prompt += f"User: {val[0]}\n" if val[1]: prompt += f"Assistant: {val[1]}\n" prompt += f"User: {symptom_input}\nAssistant:" response = "" try: # Pass the user symptoms and conversation history to the model generated = text_generator( prompt, # This is the input to the model max_new_tokens=max_tokens, num_return_sequences=1, temperature=temperature, top_p=top_p ) # Get only the assistant's response, stripping the input from the model's output response = generated[0]['generated_text'].strip().replace(prompt, "").strip() except Exception as e: response = f"Error during text generation: {str(e)}" yield responseDefine the UI with minimal user input
demo = gr.ChatInterface( respond, title="Healthcare Chatbot Assistant", description="Enter your symptoms, and the chatbot will provide health-related advice." )
if name == "main": demo.launch(server_name="10.172.2.37", server_port=5001)
The bot is not generating response properly
- Assistant