vilarin commited on
Commit
1ac84a9
·
verified ·
1 Parent(s): 827a037

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from openai import OpenAI
4
+ import os
5
+ import base64
6
+
7
+ # Set API key and organization ID from environment variables
8
+ openai.api_key = os.getenv('OPENAI_API_KEY')
9
+ openai.organization = os.getenv('OPENAI_ORG_ID')
10
+ client = OpenAI(api_key= os.getenv('OPENAI_API_KEY'), organization=os.getenv('OPENAI_ORG_ID'))
11
+
12
+ # Define the model to be used
13
+ MODEL = "gpt-4o"
14
+
15
+ def process_text(text_input):
16
+ if text_input:
17
+ completion = client.chat.completions.create(
18
+ model=MODEL,
19
+ messages=[
20
+ {"role": "system", "content": "You are a helpful assistant. Help me with my math homework!"},
21
+ {"role": "user", "content": f"Hello! Could you solve {text_input}?"}
22
+ ]
23
+ )
24
+ return "Assistant: " + completion.choices[0].message.content
25
+
26
+ def process_image(image_input):
27
+ if image_input is not None:
28
+ with open(image_input.name, "rb") as f:
29
+ base64_image = base64.b64encode(f.read()).decode("utf-8")
30
+ response = client.chat.completions.create(
31
+ model=MODEL,
32
+ messages=[
33
+ {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
34
+ {"role": "user", "content": [
35
+ {"type": "text", "text": "Help me understand what is it"},
36
+ {"type": "image_url", "image_url": {
37
+ "url": f"data:image/png;base64,{base64_image}"}
38
+ }
39
+ ]}
40
+ ],
41
+ temperature=0.0,
42
+ )
43
+ return response.choices[0].message.content
44
+
45
+ def main(text_input="", image_input=None):
46
+ if text_input and image_input is None:
47
+ return process_text(text_input)
48
+ elif image_input is not None:
49
+ return process_image(image_input)
50
+
51
+ iface = gr.Interface(fn=main, inputs=["text", gr.inputs.Image()], outputs="text")
52
+ iface.launch()