burhan112 commited on
Commit
914eefe
·
verified ·
1 Parent(s): 5574a92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -28,7 +28,7 @@ if not GEMINI_API_KEY:
28
  genai.configure(api_key=GEMINI_API_KEY)
29
  model = genai.GenerativeModel('gemini-2.0-flash')
30
 
31
- # Preprocess text function
32
  def preprocess_text(text):
33
  text = text.lower()
34
  text = text.replace('\n', ' ').replace('\t', ' ')
@@ -36,7 +36,7 @@ def preprocess_text(text):
36
  text = ' '.join(text.split()).strip()
37
  return text
38
 
39
- # Retrieve documents
40
  def retrieve_docs(query, k=5):
41
  query_embedding = minilm.encode([query], show_progress_bar=False)[0].astype(np.float32)
42
  distances, indices = index.search(np.array([query_embedding]), k)
@@ -44,24 +44,24 @@ def retrieve_docs(query, k=5):
44
  retrieved_docs['distance'] = distances[0]
45
  return retrieved_docs
46
 
47
- # Simplified respond function (no history)
48
  def respond(message, system_message, max_tokens, temperature, top_p):
49
- # Preprocess the user message
50
  preprocessed_query = preprocess_text(message)
51
-
52
- # Retrieve relevant documents
53
  retrieved_docs = retrieve_docs(preprocessed_query, k=5)
54
- context = "\n".join(retrieved_docs['text'].tolist())
55
-
56
- # Construct the prompt with system message and RAG context
 
 
57
  prompt = f"{system_message}\n\n"
58
  prompt += (
59
  f"Query: {message}\n"
60
  f"Relevant Context: {context}\n"
61
  f"Generate a short, concise, and to-the-point response to the query based only on the provided context."
62
  )
63
-
64
- # Generate response with Gemini
65
  response = model.generate_content(
66
  prompt,
67
  generation_config=genai.types.GenerationConfig(
@@ -76,10 +76,26 @@ def respond(message, system_message, max_tokens, temperature, top_p):
76
  answer = answer[:last_period + 1]
77
  else:
78
  answer += "."
79
-
80
- return answer
81
 
82
- # Simple Gradio Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  demo = gr.Interface(
84
  fn=respond,
85
  inputs=[
@@ -90,18 +106,12 @@ demo = gr.Interface(
90
  ),
91
  gr.Slider(minimum=1, maximum=2048, value=150, step=1, label="Max New Tokens"),
92
  gr.Slider(minimum=0.1, maximum=4.0, value=0.75, step=0.1, label="Temperature"),
93
- gr.Slider(
94
- minimum=0.1,
95
- maximum=1.0,
96
- value=0.95,
97
- step=0.05,
98
- label="Top-p (nucleus sampling)", # Included but not used by Gemini
99
- ),
100
  ],
101
- outputs=gr.Textbox(label="Diagnosis"),
102
  title="🏥 Medical Assistant",
103
  description="A simple medical assistant that diagnoses patient queries using AI and past records."
104
  )
105
 
106
  if __name__ == "__main__":
107
- demo.launch()
 
28
  genai.configure(api_key=GEMINI_API_KEY)
29
  model = genai.GenerativeModel('gemini-2.0-flash')
30
 
31
+ # Preprocess text
32
  def preprocess_text(text):
33
  text = text.lower()
34
  text = text.replace('\n', ' ').replace('\t', ' ')
 
36
  text = ' '.join(text.split()).strip()
37
  return text
38
 
39
+ # Retrieve top-k documents
40
  def retrieve_docs(query, k=5):
41
  query_embedding = minilm.encode([query], show_progress_bar=False)[0].astype(np.float32)
42
  distances, indices = index.search(np.array([query_embedding]), k)
 
44
  retrieved_docs['distance'] = distances[0]
45
  return retrieved_docs
46
 
47
+ # Generate structured response
48
  def respond(message, system_message, max_tokens, temperature, top_p):
49
+ # Preprocess and retrieve
50
  preprocessed_query = preprocess_text(message)
 
 
51
  retrieved_docs = retrieve_docs(preprocessed_query, k=5)
52
+
53
+ # Combine retrieved texts
54
+ context = "\n".join([f"- *{row['label']}* ({row['source']}): {row['text']}" for _, row in retrieved_docs.iterrows()])
55
+
56
+ # Build prompt
57
  prompt = f"{system_message}\n\n"
58
  prompt += (
59
  f"Query: {message}\n"
60
  f"Relevant Context: {context}\n"
61
  f"Generate a short, concise, and to-the-point response to the query based only on the provided context."
62
  )
63
+
64
+ # Get Gemini response
65
  response = model.generate_content(
66
  prompt,
67
  generation_config=genai.types.GenerationConfig(
 
76
  answer = answer[:last_period + 1]
77
  else:
78
  answer += "."
 
 
79
 
80
+ # Format output with Markdown
81
+ formatted_answer = f"""
82
+ **🩺 Patient Query:**
83
+ {message}
84
+
85
+ ---
86
+
87
+ **📚 Retrieved Context:**
88
+ {context}
89
+
90
+ ---
91
+
92
+ **🧠 Diagnosis / Suggestion:**
93
+ {answer}
94
+ """
95
+
96
+ return formatted_answer.strip()
97
+
98
+ # Gradio app
99
  demo = gr.Interface(
100
  fn=respond,
101
  inputs=[
 
106
  ),
107
  gr.Slider(minimum=1, maximum=2048, value=150, step=1, label="Max New Tokens"),
108
  gr.Slider(minimum=0.1, maximum=4.0, value=0.75, step=0.1, label="Temperature"),
109
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
110
  ],
111
+ outputs=gr.Markdown(label="Diagnosis"),
112
  title="🏥 Medical Assistant",
113
  description="A simple medical assistant that diagnoses patient queries using AI and past records."
114
  )
115
 
116
  if __name__ == "__main__":
117
+ demo.launch()