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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -1,23 +1,22 @@
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.")
@@ -26,14 +25,18 @@ with gr.Blocks() as demo:
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
 
38
 
39
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Set up the question-answering pipeline with DistilBERT
5
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
 
 
6
 
7
+ # Define the context for the chatbot to answer academic-related questions
8
+ context = """
9
+ You can ask me anything related to academic topics! I can help explain concepts from math, science, and other subjects.
10
+ For example, I can explain calculus, biology, physics, programming, and more! Please type in your question, and I will try to answer it as best as I can.
11
+ """
12
 
13
+ # Function that uses the QA pipeline to answer questions
14
+ def chatbot_response(question):
15
+ # Use the QA pipeline to answer the question based on the context
16
+ result = qa_pipeline(question=question, context=context)
17
+ return result["answer"]
 
 
18
 
19
+ # Define the Gradio interface
20
  with gr.Blocks() as demo:
21
  gr.Markdown("# Study Assistance Chatbot")
22
  gr.Markdown("Welcome! Ask me anything related to your academic studies.")
 
25
  with gr.Column():
26
  user_input = gr.Textbox(label="Enter your question here:")
27
  submit_button = gr.Button("Submit")
28
+
29
  with gr.Column():
30
  chatbot_output = gr.Textbox(label="Chatbot Response", interactive=False)
31
+
32
+ # Link the submit button to the chatbot response function
33
  submit_button.click(chatbot_response, inputs=user_input, outputs=chatbot_output)
34
 
35
+ # Launch the Gradio app
36
  demo.launch()
37
 
38
 
39
 
40
 
41
 
42
+