Singularity666 commited on
Commit
295c6f3
·
1 Parent(s): bb947d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -11,6 +11,7 @@ from docx import Document
11
  from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
12
  from io import BytesIO
13
 
 
14
  # Set up OpenAI API
15
  openai.api_key = "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"
16
 
@@ -18,13 +19,20 @@ openai.api_key = "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"
18
  st.markdown(
19
  """
20
  <style>
 
 
 
21
  .container {
22
  display: flex;
23
  justify-content: center;
24
  align-items: center;
 
25
  border-radius: 15px;
26
  padding: 20px;
27
  }
 
 
 
28
  </style>
29
  """,
30
  unsafe_allow_html=True,
@@ -37,12 +45,14 @@ model = CLIPModel().to(device)
37
  model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
38
  text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
39
 
 
40
  def show_predicted_caption(image):
41
  matches = predict_caption(
42
  image, model, text_embeddings, testing_df["caption"]
43
  )[0]
44
  return matches
45
 
 
46
  def generate_radiology_report(prompt):
47
  response = openai.Completion.create(
48
  engine="text-davinci-003",
@@ -54,6 +64,7 @@ def generate_radiology_report(prompt):
54
  )
55
  return response.choices[0].text.strip()
56
 
 
57
  def chatbot_response(prompt):
58
  response = openai.Completion.create(
59
  engine="text-davinci-003",
@@ -65,6 +76,7 @@ def chatbot_response(prompt):
65
  )
66
  return response.choices[0].text.strip()
67
 
 
68
  def save_as_docx(text, filename):
69
  document = Document()
70
  document.add_paragraph(text)
@@ -73,12 +85,14 @@ def save_as_docx(text, filename):
73
  output.seek(0)
74
  return output.getvalue()
75
 
 
76
  def download_link(content, filename, link_text):
77
  b64 = base64.b64encode(content).decode()
78
  href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
79
  return href
80
 
81
- st.title("RadiXGPT: An Evolution of machine doctors towrads Radiology")
 
82
  st.write("Upload Scan to get Radiological Report:")
83
 
84
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
@@ -91,8 +105,10 @@ if uploaded_file is not None:
91
  with st.spinner("Generating caption..."):
92
  image_np = np.array(image)
93
  caption = show_predicted_caption(image_np)
 
94
  st.success(f"Caption: {caption}")
95
 
 
96
  radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {caption}")
97
  container = st.container()
98
  with container:
@@ -105,13 +121,21 @@ if uploaded_file is not None:
105
  st.write("Ask any questions you have about the radiology report:")
106
 
107
  user_input = st.text_input("Enter your question:")
108
- while user_input.lower() != "thank you":
109
- if user_input:
110
- answer = chatbot_response(f"Answer to the user's question based on the generated radiology report: {user_input}")
111
- st.write(f"Bot: {answer}")
112
- user_input = st.text_input("Enter your question:")
 
 
113
  else:
114
- break
 
 
 
 
 
115
 
116
- if user_input.lower() == "thank you":
117
- st.write("Bot: You're welcome! If you have any more questions, feel free to ask.")
 
 
11
  from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
12
  from io import BytesIO
13
 
14
+
15
  # Set up OpenAI API
16
  openai.api_key = "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"
17
 
 
19
  st.markdown(
20
  """
21
  <style>
22
+ body {
23
+ background-color: transparent;
24
+ }
25
  .container {
26
  display: flex;
27
  justify-content: center;
28
  align-items: center;
29
+ background-color: rgba(255, 255, 255, 0.7);
30
  border-radius: 15px;
31
  padding: 20px;
32
  }
33
+ .stApp {
34
+ background-color: transparent;
35
+ }
36
  </style>
37
  """,
38
  unsafe_allow_html=True,
 
45
  model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
46
  text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
47
 
48
+
49
  def show_predicted_caption(image):
50
  matches = predict_caption(
51
  image, model, text_embeddings, testing_df["caption"]
52
  )[0]
53
  return matches
54
 
55
+
56
  def generate_radiology_report(prompt):
57
  response = openai.Completion.create(
58
  engine="text-davinci-003",
 
64
  )
65
  return response.choices[0].text.strip()
66
 
67
+
68
  def chatbot_response(prompt):
69
  response = openai.Completion.create(
70
  engine="text-davinci-003",
 
76
  )
77
  return response.choices[0].text.strip()
78
 
79
+ # Add this function to your code
80
  def save_as_docx(text, filename):
81
  document = Document()
82
  document.add_paragraph(text)
 
85
  output.seek(0)
86
  return output.getvalue()
87
 
88
+ # Add this function to your code
89
  def download_link(content, filename, link_text):
90
  b64 = base64.b64encode(content).decode()
91
  href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
92
  return href
93
 
94
+
95
+ st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
96
  st.write("Upload Scan to get Radiological Report:")
97
 
98
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
 
105
  with st.spinner("Generating caption..."):
106
  image_np = np.array(image)
107
  caption = show_predicted_caption(image_np)
108
+
109
  st.success(f"Caption: {caption}")
110
 
111
+ # Add the OpenAI API call here and generate the radiology report
112
  radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {caption}")
113
  container = st.container()
114
  with container:
 
121
  st.write("Ask any questions you have about the radiology report:")
122
 
123
  user_input = st.text_input("Enter your question:")
124
+ chat_history = []
125
+
126
+ if user_input:
127
+ chat_history.append({"user": user_input})
128
+
129
+ if user_input.lower() == "thank you":
130
+ st.write("Bot: You're welcome! If you have any more questions, feel free to ask.")
131
  else:
132
+ # Add the OpenAI API call here and generate the answer to the user's question
133
+ prompt = f"Answer to the user's question based on the generated radiology report: {user_input}"
134
+ for history_item in chat_history:
135
+ prompt += f"\nUser: {history_item['user']}"
136
+ if 'bot' in history_item:
137
+ prompt += f"\nBot: {history_item['bot']}"
138
 
139
+ answer = chatbot_response(prompt)
140
+ chat_history[-1]["bot"] = answer
141
+ st.write(f"Bot: {answer}")