Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables if running locally
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Streamlit web app layout
|
| 10 |
+
st.title("Gemini Model Suggestions Generator")
|
| 11 |
+
api_key = st.sidebar.text_input("Enter your GEMINI API Key", type="password")
|
| 12 |
+
|
| 13 |
+
if api_key:
|
| 14 |
+
# Configure Gemini with the user-provided API key
|
| 15 |
+
genai.configure(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
# Gemini model configuration
|
| 18 |
+
MODEL_NAME = 'gemini-1.5-pro'
|
| 19 |
+
SAFETY_SETTINGS = {
|
| 20 |
+
'HATE': 'BLOCK_NONE',
|
| 21 |
+
'HARASSMENT': 'BLOCK_NONE',
|
| 22 |
+
'SEXUAL': 'BLOCK_NONE',
|
| 23 |
+
'DANGEROUS': 'BLOCK_NONE'
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Function to generate suggestions
|
| 27 |
+
def generate_suggestions(free_text: str, patient_info: str, instructions: list) -> list:
|
| 28 |
+
responses = []
|
| 29 |
+
configs = [
|
| 30 |
+
{'instruction': instructions[0], 'temperature': 0.3, 'max_tokens': 1000},
|
| 31 |
+
{'instruction': instructions[1], 'temperature': 0.7, 'max_tokens': 1000},
|
| 32 |
+
{'instruction': instructions[2], 'temperature': 1.0, 'max_tokens': 1000}
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
# Create model instance
|
| 36 |
+
model = genai.GenerativeModel(MODEL_NAME)
|
| 37 |
+
|
| 38 |
+
for config in configs:
|
| 39 |
+
try:
|
| 40 |
+
# Construct the full prompt
|
| 41 |
+
full_prompt = f"""
|
| 42 |
+
[Free Text]: {free_text}
|
| 43 |
+
[Patient Information]: {patient_info}
|
| 44 |
+
[Instruction]: {config['instruction']}
|
| 45 |
+
Please provide your response below:
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
# Generate content
|
| 49 |
+
response = model.generate_content(
|
| 50 |
+
contents=full_prompt,
|
| 51 |
+
generation_config=genai.types.GenerationConfig(
|
| 52 |
+
temperature=config['temperature'],
|
| 53 |
+
max_output_tokens=config['max_tokens']
|
| 54 |
+
),
|
| 55 |
+
safety_settings=SAFETY_SETTINGS
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
responses.append({
|
| 59 |
+
'success': True,
|
| 60 |
+
'instruction': config['instruction'],
|
| 61 |
+
'temperature': config['temperature'],
|
| 62 |
+
'response': response.text
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
responses.append({
|
| 67 |
+
'success': False,
|
| 68 |
+
'error': str(e),
|
| 69 |
+
'instruction': config['instruction'],
|
| 70 |
+
'temperature': config['temperature']
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
return responses
|
| 74 |
+
|
| 75 |
+
# Example usage in Streamlit
|
| 76 |
+
if st.button("Generate Suggestions"):
|
| 77 |
+
free_text = st.text_area("Enter Free Text Information")
|
| 78 |
+
patient_info = st.text_area("Enter Patient Information")
|
| 79 |
+
instructions = [
|
| 80 |
+
"a) Provide possible diagnoses in bullet points, b) Suggest lifestyle modifications, c) Recommend diagnostic tests",
|
| 81 |
+
"a) Provide possible course of action for physio in bullet points, b) Suggest lifestyle modifications that can be achieved by the patient",
|
| 82 |
+
"a) General preventive advice for the patient through physio routines, b) Suggest lifestyle modifications that can be achieved by the patient"
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
results = generate_suggestions(free_text, patient_info, instructions)
|
| 86 |
+
|
| 87 |
+
st.write("Generated Suggestions:")
|
| 88 |
+
for idx, result in enumerate(results, 1):
|
| 89 |
+
st.markdown(f"### Suggestion {idx}:")
|
| 90 |
+
st.write(f"Temperature: {result['temperature']}")
|
| 91 |
+
st.write(f"Instruction: {result['instruction']}")
|
| 92 |
+
|
| 93 |
+
if result['success']:
|
| 94 |
+
st.write(f"Response:\n{result['response']}")
|
| 95 |
+
else:
|
| 96 |
+
st.error(f"Error: {result['error']}")
|
| 97 |
+
|
| 98 |
+
else:
|
| 99 |
+
st.sidebar.warning("Please enter a valid API key.")
|