import gradio as gr import replicate import os import random import openai import numpy as np from PIL import Image import requests import io import base64 import zipfile from transformers import pipeline # Set API tokens os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo" # Initialize the Replicate client rep_client = replicate.Client() # Set your OpenAI API key OPENAI_API_KEY = "sk-proj-5iy4bwrqAW8GpguiEawaT3BlbkFJ8p88lLSjOCeDbxWsAOlr" openai.api_key = OPENAI_API_KEY # Load sentiment analysis model sentiment_analysis = pipeline('sentiment-analysis') predefined_prompts = [ "Missing bolts on railway track", "Cracks on railway track", "Overgrown vegetation near railway track", "Broken railings on railway bridge", "Debris on railway track", "Damaged railway platform" ] def analyze_feedback(feedback): result = sentiment_analysis(feedback)[0] sentiment = result['label'] score = result['score'] if sentiment == "POSITIVE": return f"Thank you for your positive feedback! Your satisfaction score is {score}." else: return f"Sorry to hear that. We are trying to improve based on your feedback. Your dissatisfaction score is {score}." def generate_variations(base_prompt, number_of_variations): locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"] sizes = ["small", "medium", "large", "tiny", "huge"] weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"] variations = [] for _ in range(number_of_variations): location = random.choice(locations) size = random.choice(sizes) weather = random.choice(weather_conditions) full_prompt = f"{base_prompt}, with a {size} defect {location}, observed {weather}." variations.append(full_prompt) return variations def generate_images(prompts): images = [] for prompt in prompts: try: prediction = rep_client.predictions.create( version="ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4", input={"prompt": prompt, "scheduler": "K_EULER"} ) prediction.wait() if prediction.status == "succeeded" and prediction.output: images.append(prediction.output[0]) else: images.append("Failed to generate image.") except Exception as e: images.append(f"Error: {str(e)}") return images # UI creation with gr.Blocks() as app: with gr.Tabs("Prompt Input"): with gr.Tab("Generate Images"): prompt_input = gr.Dropdown(choices=predefined_prompts, label="Select a defect prompt") number_input = gr.Number(label="Number of images", value=1, minimum=1, maximum=10) generate_button = gr.Button("Generate") gallery = gr.Gallery(label="Generated Images") generate_button.click( fn=lambda prompt, num: generate_images(generate_variations(prompt, num)), inputs=[prompt_input, number_input], outputs=gallery ) with gr.Tab("Custom Defect"): custom_prompt_input = gr.Textbox(label="Custom Defect") number_input_custom = gr.Number(label="Number of images to generate", value=1, minimum=1, maximum=10) submit_button_custom = gr.Button("Generate") image_outputs_custom = gr.Gallery() submit_button_custom.click( fn=lambda prompt, num: generate_images(generate_variations(prompt, num)), inputs=[custom_prompt_input, number_input_custom], outputs=image_outputs_custom ) feedback_input = gr.Textbox(label="Enter your feedback", placeholder="Write your feedback here...") like_button = gr.Button(value="👍 Like") dislike_button = gr.Button(value="👎 Dislike") feedback_result = gr.Textbox(label="System Response", interactive=False) refresh_button = gr.Button("Refresh Page") like_button.click(lambda x: analyze_feedback(x), inputs=feedback_input, outputs=feedback_result) dislike_button.click(lambda x: analyze_feedback(x), inputs=feedback_input, outputs=feedback_result) refresh_button.click(lambda: gr.update(reload_browser=True)) if __name__ == "__main__": app.launch()