Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grdef chatbot_response(user_input):# Handle basic greetingif user_input.lower() in ["hello", "hi"]:return "Hello! How can I assist you today?"# Add more conditions for different queries hereelif "supervised learning" in user_input.lower():return "Supervised learning is a machine learning approach where models are trained using labeled data."# If no predefined match, ask for more clarificationelse:return "I'm here to assist with academic questions. Please specify what you'd like help with."with gr.Blocks() as demo:gr.Markdown("### Study Assistance Chatbot")gr.Markdown("Welcome! Ask me anything related to your academic studies.")with gr.Row():with gr.Column():user_input = gr.Textbox(label="Enter your question here:")submit_button = gr.Button("Submit")with gr.Column():chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output)demo.launch()from datasets import load_dataset# Load a sample dataset from Hugging Facedataset = load_dataset("squad") # you can replace "squad" with any dataset you're using# Print the first few entries to verify that it’s loadedprint(dataset["train"][0]) # Prints the first example from the training setfrom transformers import pipeline, AutoModelForCausalLM, AutoTokenizer# Load pre-trained GPT-2 model and tokenizer from Hugging Facemodel_name = "gpt2" # You can use other models such as 'distilgpt2' for faster responses# Initialize tokenizer and modeltokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)# Create a pipeline for text generationgenerator = pipeline("text-generation", model=model, tokenizer=tokenizer)def chatbot_response(user_input):# Generate a response using the modelresponse = generator(user_input, max_length=100, num_return_sequences=1)# Extract and return the generated textreturn response[0]['generated_text']12345678910111213141516171819202122232425262728293031323334353637import gradio as grfrom transformers import pipeline, AutoModelForCausalLM, AutoTokenizer# Initialize pre-trained model and tokenizermodel_name = "gpt2" # You can change this to another model if neededtokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name)# Create a pipeline for text generationgenerator = pipeline("text-generation", model=model, tokenizer=tokenizer)# Chatbot response functiondef chatbot_response(user_input):# Generate a response using the modelresponse = generator(user_input, max_length=100, num_return_sequences=1, temperature=0.7, top_k=50)# Extract and return the generated textreturn response[0]['generated_text']# Create the Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Study Assistance Chatbot")gr.Markdown("Welcome! Ask me anything related to your academic studies.")with gr.Row():with gr.Column():user_input = gr.Textbox(label="Enter your question here:")submit_button = gr.Button("Submit")with gr.Column():chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output)demo.launch()
app.py+17-36