Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
|
|
|
|
|
|
| 7 |
import zipfile
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def image_to_data_url(image):
|
| 10 |
buffered = io.BytesIO()
|
| 11 |
image.save(buffered, format="PNG")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import replicate
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import openai
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
+
import requests
|
| 9 |
+
import io
|
| 10 |
+
import base64
|
| 11 |
import zipfile
|
| 12 |
|
| 13 |
+
# Set API tokens
|
| 14 |
+
os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
|
| 15 |
+
# Initialize the Replicate client
|
| 16 |
+
rep_client = replicate.Client()
|
| 17 |
+
|
| 18 |
+
# Set your OpenAI API key
|
| 19 |
+
OPENAI_API_KEY = "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"
|
| 20 |
+
openai.api_key = OPENAI_API_KEY
|
| 21 |
+
|
| 22 |
+
# Predefined prompts for the dropdown
|
| 23 |
+
predefined_prompts = [
|
| 24 |
+
"Missing bolts on railway track",
|
| 25 |
+
"Cracks on railway track",
|
| 26 |
+
"Overgrown vegetation near railway track",
|
| 27 |
+
"Broken railings on railway bridge",
|
| 28 |
+
"Debris on railway track",
|
| 29 |
+
"Damaged railway platform"
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:personal::99NsSAeQ'):
|
| 33 |
+
response = openai.ChatCompletion.create(
|
| 34 |
+
model=model_name,
|
| 35 |
+
messages=[
|
| 36 |
+
{
|
| 37 |
+
"role": "system",
|
| 38 |
+
"content": "The assistant is knowledgeable about rail defects and can answer questions related to them.",
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": question,
|
| 43 |
+
}
|
| 44 |
+
],
|
| 45 |
+
)
|
| 46 |
+
return response.choices[0].message['content']
|
| 47 |
+
|
| 48 |
+
# Function to generate variations enhanced by the GPT model
|
| 49 |
+
def generate_variations(base_prompt, number_of_variations):
|
| 50 |
+
locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
|
| 51 |
+
sizes = ["small", "medium", "large", "tiny", "huge"]
|
| 52 |
+
weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
|
| 53 |
+
|
| 54 |
+
variations = []
|
| 55 |
+
for _ in range(number_of_variations):
|
| 56 |
+
location = random.choice(locations)
|
| 57 |
+
size = random.choice(sizes)
|
| 58 |
+
weather = random.choice(weather_conditions)
|
| 59 |
+
|
| 60 |
+
# Enhance the base prompt with the GPT model
|
| 61 |
+
enhanced_prompt = base_prompt
|
| 62 |
+
full_prompt = f"{enhanced_prompt}, with a {size} defect {location}, observed {weather}."
|
| 63 |
+
variations.append(full_prompt)
|
| 64 |
+
return variations
|
| 65 |
+
|
| 66 |
def image_to_data_url(image):
|
| 67 |
buffered = io.BytesIO()
|
| 68 |
image.save(buffered, format="PNG")
|