NikilDGr8 commited on
Commit
4fb67f7
1 Parent(s): 23a8181

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -38
app.py CHANGED
@@ -1,9 +1,5 @@
1
- import time
2
  import gradio as gr
3
- from transformers import pipeline
4
-
5
- # Load the generation model
6
- generator = pipeline("text2text-generation", model="t5-small")
7
 
8
  # Define the questions
9
  questions = [
@@ -29,41 +25,57 @@ questions = [
29
  "What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
30
  ]
31
 
32
- # Function to generate answers from context
33
- def generate_answers(context):
34
- start_time = time.time()
35
- answers = []
36
- for question in questions:
37
- # Generate the answer using the model
38
- answer = generator(f"Context: {context}\nQuestion: {question}", max_length=50)[0]['generated_text']
39
- answers.append(answer)
40
- end_time = time.time()
41
- generation_time = end_time - start_time
42
- print(f"Time taken to generate answers: {generation_time:.2f} seconds")
43
- return answers
 
 
 
 
 
 
 
 
44
 
45
- # Gradio interface
46
- def main(context):
47
- answers = generate_answers(context)
48
- return answers
 
 
 
 
 
 
49
 
50
- # Gradio app layout
51
- with gr.Blocks() as demo:
52
- context = gr.Textbox(lines=5, placeholder="Enter the context here", label="Context")
53
- generate_button = gr.Button("Generate Answers")
54
- output_table = gr.Dataframe(headers=["Question", "Answer"], row_count=20, col_count=2, visible=False)
55
- save_button = gr.Button("Save Answers", visible=False)
56
 
57
- def generate_and_show_table(context):
58
- answers = main(context)
59
- data = [[question, answer] for question, answer in zip(questions, answers)]
60
- generate_button.update(interactive=False)
61
- output_table.update(data, visible=True)
62
- save_button.update(visible=True)
63
- return data
64
 
65
- generate_button.click(generate_and_show_table, inputs=context, outputs=[output_table])
66
- save_button.click(lambda: "Answers displayed in the table", inputs=None, outputs=gr.Textbox(label="Status"))
 
 
 
 
 
 
 
 
67
 
68
- # Launch the app
69
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, set_seed
 
 
 
3
 
4
  # Define the questions
5
  questions = [
 
25
  "What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
26
  ]
27
 
28
+ # Initialize answers
29
+ answers = {q: '' for q in questions}
30
+
31
+ # Define the default context
32
+ default_context = (
33
+ "My child, Sarah, is studying in the 3rd grade. She is 8 years old and is a girl. "
34
+ "Sarah attends Greenwood Elementary School, located in Springfield. "
35
+ "Her guardians are myself, Emma Johnson, and her father, Michael Johnson.\n\n"
36
+ "Sarah's chief complaint regarding her oral health is that she has been experiencing some discomfort due to a cavity in her molar. "
37
+ "Other than that, she has no significant medication history. She has no relevant medical history to report. "
38
+ "Sarah does not take any medications regularly.\n\n"
39
+ "Her last dental visit was about a year ago, so this is her second visit. "
40
+ "Sarah has a habit of occasionally biting her nails, but she doesn't have any other habits like thumb sucking or tongue thrusting.\n\n"
41
+ "Sarah brushes her teeth twice daily. She does not experience bleeding gums. "
42
+ "Sarah has not experienced early childhood caries. Unfortunately, she does have tooth decay present in tooth numbers 14 and 15. "
43
+ "None of her teeth have been fractured. There is no pre-shedding mobility of teeth to report.\n\n"
44
+ "Sarah does not have malocclusion. She does not experience any pain, swelling, or abscess at the moment. "
45
+ "There are no other significant findings to note.\n\n"
46
+ "As for the treatment plan, I would recommend a filling for the decayed teeth."
47
+ )
48
 
49
+ # Load the GPT-2 model for text generation
50
+ generator = pipeline('text-generation', model='gpt2')
51
+
52
+ # Function to generate answers
53
+ def generate_answers(context):
54
+ prompt = context + "\n\n" + "\n\n".join(questions)
55
+ set_seed(42) # Ensure reproducibility
56
+ generated_text = generator(prompt, max_length=300, num_return_sequences=1)[0]['generated_text']
57
+ generated_answers = generated_text.split("\n\n")
58
+ return generated_answers
59
 
60
+ # Function to display the form
61
+ def form_display():
62
+ return [gr.outputs.Textbox(label="Generated Answers", type="text")]
 
 
 
63
 
64
+ # Function to submit the form
65
+ def form_submit(context):
66
+ generated_answers = generate_answers(context)
67
+ return generated_answers
 
 
 
68
 
69
+ # Create Gradio interface
70
+ iface = gr.Interface(
71
+ fn=form_submit,
72
+ inputs=gr.inputs.Textbox(label="Context", type="text", default=default_context, lines=20),
73
+ outputs=form_display(),
74
+ title="Dental Health Record Form with GPT-2",
75
+ description="Fill out the context and click submit to generate answers to the questions.",
76
+ examples=[default_context],
77
+ allow_flagging=False
78
+ )
79
 
80
+ # Launch the interface
81
+ iface.launch()