MissieMcCown commited on
Commit
b26fbd2
·
verified ·
1 Parent(s): eca3380

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -36
app.py CHANGED
@@ -1,56 +1,37 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
 
3
  def chatbot_response(user_input):
4
- # Handle basic greeting
5
- if user_input.lower() in ["hello", "hi"]:
6
- return "Hello! How can I assist you today?"
7
-
8
- # Add more conditions for different queries here
9
- elif "supervised learning" in user_input.lower():
10
- return "Supervised learning is a machine learning approach where models are trained using labeled data."
11
 
12
- # If no predefined match, ask for more clarification
13
- else:
14
- return "I'm here to assist with academic questions. Please specify what you'd like help with."
15
 
 
16
  with gr.Blocks() as demo:
17
- gr.Markdown("### Study Assistance Chatbot")
18
  gr.Markdown("Welcome! Ask me anything related to your academic studies.")
19
 
20
  with gr.Row():
21
  with gr.Column():
22
  user_input = gr.Textbox(label="Enter your question here:")
23
  submit_button = gr.Button("Submit")
24
-
25
  with gr.Column():
26
  chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)
27
-
28
  submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output)
29
 
30
  demo.launch()
31
- from datasets import load_dataset
32
-
33
- # Load a sample dataset from Hugging Face
34
- dataset = load_dataset("squad") # you can replace "squad" with any dataset you're using
35
-
36
- # Print the first few entries to verify that it’s loaded
37
- print(dataset["train"][0]) # Prints the first example from the training set
38
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
39
- # Load pre-trained GPT-2 model and tokenizer from Hugging Face
40
- model_name = "gpt2" # You can use other models such as 'distilgpt2' for faster responses
41
-
42
- # Initialize tokenizer and model
43
- tokenizer = AutoTokenizer.from_pretrained(model_name)
44
- model = AutoModelForCausalLM.from_pretrained(model_name)
45
-
46
- # Create a pipeline for text generation
47
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
48
- def chatbot_response(user_input):
49
- # Generate a response using the model
50
- response = generator(user_input, max_length=100, num_return_sequences=1)
51
-
52
- # Extract and return the generated text
53
- return response[0]['generated_text']
54
 
55
 
56
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Initialize pre-trained model and tokenizer
5
+ model_name = "gpt2" # You can change this to another model if needed
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ # Create a pipeline for text generation
10
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
12
+ # Chatbot response function
13
  def chatbot_response(user_input):
14
+ # Generate a response using the model
15
+ response = generator(user_input, max_length=100, num_return_sequences=1, temperature=0.7, top_k=50)
 
 
 
 
 
16
 
17
+ # Extract and return the generated text
18
+ return response[0]['generated_text']
 
19
 
20
+ # Create the Gradio interface
21
  with gr.Blocks() as demo:
22
+ gr.Markdown("# Study Assistance Chatbot")
23
  gr.Markdown("Welcome! Ask me anything related to your academic studies.")
24
 
25
  with gr.Row():
26
  with gr.Column():
27
  user_input = gr.Textbox(label="Enter your question here:")
28
  submit_button = gr.Button("Submit")
 
29
  with gr.Column():
30
  chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)
31
+
32
  submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output)
33
 
34
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
 
37