AkashMnd commited on
Commit
0b9e99a
1 Parent(s): f9b79a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import gradio as gr
3
+ import requests
4
+
5
+ def encode_image(image_file):
6
+ with open(image_file.name, "rb") as img_file:
7
+ return base64.b64encode(img_file.read()).decode('utf-8')
8
+
9
+ def send_to_openai(api_key, image_file):
10
+ base64_image = encode_image(image_file)
11
+
12
+ headers = {
13
+ "Content-Type": "application/json",
14
+ "Authorization": f"Bearer {api_key}"
15
+ }
16
+
17
+ payload = {
18
+ "model": "gpt-4-vision-preview",
19
+ "messages": [
20
+ {
21
+ "role": "user",
22
+ "content": [
23
+ {
24
+ "type": "text",
25
+ "text": "Answer in three words only, does the image uploaded have a healthy rice leaf - Yes or No , does the image uploaded have a rice leaf with brown spots - Yes or No , does the image uploaded have a rice leaf have yellowish discoloration in some spots - Yes or No , DO NOT RESPOND IN MORE THAN THREE WORDS "
26
+ },
27
+ {
28
+ "type": "image_url",
29
+ "image_url": {
30
+ "url": f"data:image/jpeg;base64,{base64_image}"
31
+ }
32
+ }
33
+ ]
34
+ }
35
+ ],
36
+ "max_tokens": 300
37
+ }
38
+
39
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
40
+ # Extract words from the assistant's response
41
+ assistant_response = response.json()['choices'][0]['message']['content']
42
+ words = assistant_response.split('\n')
43
+
44
+ return ' '.join(words)
45
+
46
+ iface = gr.Interface(send_to_openai, ["text", "file"], "text")
47
+ iface.launch()