Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
from fastai.vision.all import *
|
3 |
-
import skimage
|
4 |
import openai
|
|
|
|
|
5 |
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
-
# Load the model
|
9 |
learn = load_learner('model.pkl')
|
10 |
|
11 |
-
# Define the labels
|
12 |
labels = learn.dls.vocab
|
13 |
|
14 |
-
# Define a function for generating text
|
15 |
-
def generate_text(prompt):
|
16 |
-
response = openai.Completion.create(
|
17 |
-
engine="davinci",
|
18 |
-
prompt=prompt,
|
19 |
-
max_tokens=1024,
|
20 |
-
n=1,
|
21 |
-
stop=None,
|
22 |
-
temperature=0.7,
|
23 |
-
)
|
24 |
-
return response.choices[0].text.strip()
|
25 |
-
|
26 |
-
# Define a function to handle user queries
|
27 |
-
def handle_query(query, chat_history):
|
28 |
-
response = openai.ChatCompletion.create(
|
29 |
-
model="gpt-3.5-turbo",
|
30 |
-
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
31 |
-
{"role": "user", "content": query}] + chat_history
|
32 |
-
)
|
33 |
-
return response.choices[0].message['content']
|
34 |
-
|
35 |
# Define the prediction function
|
36 |
def predict(img):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
#
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from fastai.vision.all import *
|
|
|
3 |
import openai
|
4 |
+
import os
|
5 |
+
|
6 |
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
|
9 |
+
# Load your trained model (you should replace 'model.pkl' with the path to your model file)
|
10 |
learn = load_learner('model.pkl')
|
11 |
|
12 |
+
# Define the labels for the output
|
13 |
labels = learn.dls.vocab
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Define the prediction function
|
16 |
def predict(img):
|
17 |
+
img = PILImage.create(img)
|
18 |
+
pred, pred_idx, probs = learn.predict(img)
|
19 |
+
prediction = {labels[i]: float(probs[i]) for i in range(len(labels))}
|
20 |
+
|
21 |
+
# Now generate a chat/text response based on the model's prediction.
|
22 |
+
chat_prompt = f"The image likely depicts the following: {pred}. What can I help you with next?"
|
23 |
+
|
24 |
+
# Ensure that you have set the OPENAI_API_KEY environment variable,
|
25 |
+
# as we will use it to interact with OpenAI's GPT-3 model.
|
26 |
+
response = openai.Completion.create(
|
27 |
+
engine="text-davinci-003", # Adjust the engine as needed for your use-case
|
28 |
+
prompt=chat_prompt,
|
29 |
+
max_tokens=1024,
|
30 |
+
n=1,
|
31 |
+
stop=None,
|
32 |
+
temperature=0.7,
|
33 |
+
)
|
34 |
+
text_response = response.choices[0].text.strip()
|
35 |
+
|
36 |
+
return prediction, text_response
|
37 |
+
|
38 |
+
# Create examples list by specifying the paths to the example images
|
39 |
+
examples = ["path/to/example1.jpg", "path/to/example2.jpg"] # replace with actual image paths
|
40 |
+
|
41 |
+
# Define the Gradio interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=predict,
|
44 |
+
inputs=gr.Image(shape=(512, 512)),
|
45 |
+
outputs=[gr.Label(num_top_classes=3), gr.Textbox(label="GPT-3 Response")],
|
46 |
+
examples=examples,
|
47 |
+
enable_queue=True # This is optional and only necessary if you're hosting under heavy traffic
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the Gradio app
|
51 |
+
iface.launch()
|