genai-girl commited on
Commit
90822c8
·
verified ·
1 Parent(s): 8d019d8

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +91 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install -q google-generativeai==0.3.1
2
+ #pip install -q gradio
3
+
4
+ import google.generativeai as genai
5
+ from pathlib import Path
6
+ import gradio as gr
7
+
8
+ # Set up the model
9
+ generation_config = {
10
+ "temperature": 0,
11
+ "top_p": 1,
12
+ "top_k": 32,
13
+ "max_output_tokens": 4096,
14
+ }
15
+
16
+ safety_settings = [
17
+ {
18
+ "category": "HARM_CATEGORY_HARASSMENT",
19
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
20
+ },
21
+ {
22
+ "category": "HARM_CATEGORY_HATE_SPEECH",
23
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
24
+ },
25
+ {
26
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
27
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
28
+ },
29
+ {
30
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
31
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
32
+ }
33
+ ]
34
+
35
+ genai.configure(api_key = "AIzaSyBzXvmkjzz4RYOo7W2RjKjK-vsONkfvfM0")
36
+
37
+ model = genai.GenerativeModel(model_name = "gemini-pro-vision",
38
+ generation_config = generation_config,
39
+ safety_settings = safety_settings)
40
+
41
+ def input_image_setup(file_loc):
42
+ if not (img := Path(file_loc)).exists():
43
+ raise FileNotFoundError(f"Could not find image: {img}")
44
+
45
+ image_parts = [
46
+ {
47
+ "mime_type": "image/jpeg",
48
+ "data": Path(file_loc).read_bytes()
49
+ }
50
+ ]
51
+ return image_parts
52
+
53
+ def generate_gemini_response(input_prompt,text_input, image_loc):
54
+
55
+ image_prompt = input_image_setup(image_loc)
56
+ prompt_parts = [input_prompt + text_input ,image_prompt[0]]
57
+ response = model.generate_content(prompt_parts)
58
+ return response.text
59
+
60
+ #input_prompt = """ This image shows a person with an injury or the visible symptoms on the body. Please analyze the image and the prompted message and identify the possible injury. Based on your analysis, suggest appropriate first aid measures that can be taken to address the injury. """
61
+
62
+ input_prompt = """ This image shows a person with an injury or visible symptoms on the body.
63
+ Please analyze the image along with the prompted message and identify the possible injury.
64
+ Based on your analysis, suggest appropriate first aid measures that can be taken to address the injury.
65
+ And also provides useful website links (Indian links only) which are valid and existing
66
+ and which can give more information regarding the first aid for the injury and also
67
+ Ensure the target website links are publicly accessible and not blocked by firewalls or login requirements.
68
+ And also provide contact information of the helpline for the next steps. And constrain the response to India.
69
+ The prompted message is """
70
+
71
+ def upload_file(files, text_input): # Added text_input parameter
72
+ file_paths = [file.name for file in files]
73
+ if file_paths:
74
+ response = generate_gemini_response(input_prompt,text_input, file_paths[0]) # Pass text_input
75
+ return file_paths[0], response
76
+
77
+ with gr.Blocks() as demo:
78
+ header = gr.Label("Please let us know about your injury and gen ai will try to help you to find the best first aid:")
79
+ text_input = gr.Textbox(label="Explain a bit more about your injury") # New text box
80
+ image_output = gr.Image()
81
+ upload_button = gr.UploadButton("Click to upload an image of the injury",
82
+ file_types=["image"],
83
+ file_count="multiple")
84
+
85
+ file_output = gr.Textbox(label="First Aid process")
86
+ combined_output = [image_output, file_output]
87
+
88
+ upload_button.upload(upload_file, [upload_button, text_input], combined_output) # Pass text_input
89
+
90
+ demo.launch(debug=True)
91
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ google-generativeai==0.3.1
2
+ gradio