git.name commited on
Commit
bff9622
·
1 Parent(s): c0ad4b9

updating th model and the task to question answer

Browse files
Files changed (3) hide show
  1. main.py +4 -4
  2. static/index.html +7 -4
  3. static/script.js +6 -5
main.py CHANGED
@@ -8,7 +8,7 @@ from transformers import pipeline
8
  app = FastAPI()
9
 
10
  # Initialize the pipeline
11
- pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
12
 
13
  # Serve static files
14
  app.mount("/static", StaticFiles(directory="static", html=True), name="static")
@@ -20,7 +20,7 @@ async def read_index():
20
 
21
  # Endpoint for text-to-text generation
22
  @app.get("/generate-text")
23
- async def generate_text(input: str):
24
- output = pipe_flan(input)
25
  print(output)
26
- return {"output": output[0]["generated_text"]}
 
8
  app = FastAPI()
9
 
10
  # Initialize the pipeline
11
+ question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')
12
 
13
  # Serve static files
14
  app.mount("/static", StaticFiles(directory="static", html=True), name="static")
 
20
 
21
  # Endpoint for text-to-text generation
22
  @app.get("/generate-text")
23
+ async def generate_text(question: str, context: str):
24
+ output = question_answerer(question=question, context=context)
25
  print(output)
26
+ return {"output": f"Answer: '{output['answer']}', score: {round(output['score'], 4)}, start: {output['start']}, end: {output['end']}"}
static/index.html CHANGED
@@ -8,19 +8,22 @@
8
  <body>
9
  <main>
10
  <section id="text-gen">
11
- <h2>Text generation using Flan T5</h2>
12
  <p>
13
  Model:
14
  <a
15
- href="https://huggingface.co/google/flan-t5-small"
16
  rel="noreferrer"
17
- target="_blank">google/flan-t5-small
18
  </a>
19
  </p>
20
  <form class="text-gen-form" id="textGenForm">
 
 
 
21
  <label for="text-gen-input">Text prompt:</label>
22
  <input
23
- id="text-gen-input"
24
  name="text"
25
  type="text"
26
  placeholder="Ask me anything"
 
8
  <body>
9
  <main>
10
  <section id="text-gen">
11
+ <h2>Text generation using GPT2</h2>
12
  <p>
13
  Model:
14
  <a
15
+ href="https://huggingface.co/distilbert/distilbert-base-cased-distilled-squad"
16
  rel="noreferrer"
17
+ target="_blank">distilbert-base-cased-distilled-squad
18
  </a>
19
  </p>
20
  <form class="text-gen-form" id="textGenForm">
21
+ <label for="text-gen-context">Text context:</label>
22
+ <textarea id="text-gen-context" ></textarea>
23
+ <br />
24
  <label for="text-gen-input">Text prompt:</label>
25
  <input
26
+ id="text-gen-question"
27
  name="text"
28
  type="text"
29
  placeholder="Ask me anything"
static/script.js CHANGED
@@ -1,9 +1,9 @@
1
  document.addEventListener("DOMContentLoaded", () => {
2
  const textGenForm = document.querySelector(".text-gen-form");
3
 
4
- const translateText = async (text) => {
5
  try {
6
- const response = await fetch(`generate-text?input=${encodeURIComponent(text)}`);
7
  if (!response.ok) {
8
  throw new Error('Network response was not ok.');
9
  }
@@ -17,13 +17,14 @@ document.addEventListener("DOMContentLoaded", () => {
17
 
18
  textGenForm.addEventListener("submit", async (event) => {
19
  event.preventDefault();
20
- const textGenInput = document.getElementById("text-gen-input");
 
21
  const textGenParagraph = document.querySelector(".text-gen-output");
22
 
23
  // Display a loading message or similar feedback
24
- textGenParagraph.textContent = "Generating text...";
25
 
26
- const output = await translateText(textGenInput.value);
27
  textGenParagraph.textContent = output;
28
  });
29
  });
 
1
  document.addEventListener("DOMContentLoaded", () => {
2
  const textGenForm = document.querySelector(".text-gen-form");
3
 
4
+ const translateText = async (question, context) => {
5
  try {
6
+ const response = await fetch(`generate-text?question=${encodeURIComponent(question)}&context=${encodeURIComponent(context)}`);
7
  if (!response.ok) {
8
  throw new Error('Network response was not ok.');
9
  }
 
17
 
18
  textGenForm.addEventListener("submit", async (event) => {
19
  event.preventDefault();
20
+ const textGenContext = document.getElementById("text-gen-context");
21
+ const textGenQuestion = document.getElementById("text-gen-question");
22
  const textGenParagraph = document.querySelector(".text-gen-output");
23
 
24
  // Display a loading message or similar feedback
25
+ textGenParagraph.textContent = "Generating answer...";
26
 
27
+ const output = await translateText(textGenQuestion.value, textGenContext.value);
28
  textGenParagraph.textContent = output;
29
  });
30
  });