Manishh-07 commited on
Commit
a34321f
·
verified ·
1 Parent(s): da0c9a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -111
app.py CHANGED
@@ -1,120 +1,33 @@
1
  import gradio as gr
2
- import torch
3
- import json
4
- import time
5
  import subprocess
6
  import os
7
- from transformers import AutoTokenizer, AutoModelForCausalLM
8
 
9
- # Initialize DeepSeek
10
- model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
11
- tokenizer = AutoTokenizer.from_pretrained(model_name)
12
- model = AutoModelForCausalLM.from_pretrained(model_name)
13
- model.to("cpu")
14
-
15
- def generate_prompts_and_image(input_text):
16
- start_time = time.time()
17
-
18
- # Generate prompts with DeepSeek
19
- prompt = f"""
20
- Input: "{input_text}"
21
- Task: Generate concise 'Positive' and 'Negative' AI image prompts for Stable Diffusion based on the input above. Output the prompts directly, no extra text or examples.
22
- """
23
- inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
24
- outputs = model.generate(**inputs, max_new_tokens=50, temperature=0.7, top_p=0.9, do_sample=True)
25
- response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
26
- if response.startswith(prompt):
27
- response = response[len(prompt):].strip()
28
-
29
- # Split response into positive and negative (assuming two lines)
30
- lines = [line.strip() for line in response.split("\n") if line.strip()]
31
- positive = lines[0] if lines else "No positive prompt generated"
32
- negative = lines[1] if len(lines) > 1 else "No negative prompt generated"
33
-
34
- # Debug: Check if ComfyUI and model exist
35
- comfyui_dir = os.path.join(os.path.dirname(__file__), "ComfyUI")
36
- checkpoint_path = os.path.join(comfyui_dir, "models", "checkpoints", "v1-5-pruned-emaonly-fp16.safetensors")
37
- if not os.path.exists(comfyui_dir):
38
- return {"error": f"ComfyUI directory not found at {comfyui_dir}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
39
- if not os.path.exists(checkpoint_path):
40
- return {"error": f"Checkpoint not found at {checkpoint_path}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
41
-
42
- # Load and debug ComfyUI workflow
43
- workflow_path = "workflow.json"
44
- temp_workflow_path = "temp_workflow.json"
45
- if not os.path.exists(workflow_path):
46
- return {"error": "workflow.json not found", "time_taken": f"{time.time() - start_time:.2f} seconds"}
47
-
48
- with open(workflow_path, "r") as f:
49
- workflow = json.load(f)
50
-
51
- # Debug: Print workflow nodes to verify IDs
52
- print("Workflow nodes:", json.dumps(workflow["nodes"], indent=2))
53
-
54
- # Use hardcoded node IDs from workflow.json (6 for positive, 7 for negative)
55
- positive_id, negative_id = 6, 7 # Hardcoded based on your workflow.json
56
  try:
57
- if str(positive_id) not in workflow or str(negative_id) not in workflow:
58
- return {"error": f"Node IDs {positive_id} or {negative_id} not found in workflow.json", "time_taken": f"{time.time() - start_time:.2f} seconds"}
59
-
60
- # Inject prompts into nodes 6 (positive) and 7 (negative)
61
- workflow[str(positive_id)]["widgets_values"][0] = positive # Positive prompt (CLIPTextEncode)
62
- workflow[str(negative_id)]["widgets_values"][0] = negative # Negative prompt (CLIPTextEncode)
63
- except KeyError as e:
64
- return {"error": f"Error injecting prompts: {str(e)}", "time_taken": f"{time.time() - start_time:.2f} seconds"}
65
-
66
- # Save temporary workflow
67
- with open(temp_workflow_path, "w") as f:
68
- json.dump(workflow, f)
69
-
70
- # Run ComfyUI via subprocess
71
- comfyui_main = os.path.join(comfyui_dir, "main.py")
72
- output_dir = os.path.join(comfyui_dir, "output") # ComfyUI default output dir
73
- os.makedirs(output_dir, exist_ok=True)
74
-
75
- try:
76
- # Note: ComfyUI's main.py doesn't use --input-directory or --output-directory directly; it reads from workflow
77
- result = subprocess.run(
78
- ["python", comfyui_main],
79
- cwd=comfyui_dir,
80
- capture_output=True,
81
- text=True,
82
- check=True
83
- )
84
- # ComfyUI saves images as ComfyUI_<timestamp>.png in output/
85
- image_files = [f for f in os.listdir(output_dir) if f.startswith("ComfyUI") and f.endswith(".png")]
86
- if not image_files:
87
- image = f"ComfyUI ran but no image found: {result.stdout}\n{result.stderr}"
88
- else:
89
- image = os.path.join(output_dir, image_files[-1]) # Use the latest image
90
- except subprocess.CalledProcessError as e:
91
- image = f"ComfyUI failed: {e.stdout}\n{e.stderr}"
92
-
93
- elapsed_time = time.time() - start_time
94
- return {
95
- "positive": positive,
96
- "negative": negative,
97
- "image": image,
98
- "time_taken": f"{elapsed_time:.2f} seconds"
99
- }
100
 
101
- def gradio_interface(input_text):
102
- result = generate_prompts_and_image(input_text)
103
- return result["image"], json.dumps({
104
- "positive": result["positive"],
105
- "negative": result["negative"],
106
- "time_taken": result["time_taken"]
107
- }, indent=2)
108
-
109
- demo = gr.Interface(
110
- fn=gradio_interface,
111
- inputs=gr.Textbox(label="Input Text", lines=10, placeholder="Paste your input text here..."),
112
- outputs=[
113
- gr.Image(label="Generated Image"),
114
- gr.Textbox(label="Generated Prompts (JSON)")
115
- ],
116
- title="Prompt and Image Generator"
117
  )
118
 
 
119
  if __name__ == "__main__":
120
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
 
 
2
  import subprocess
3
  import os
 
4
 
5
+ # Define the function to run hr.py
6
+ def run_workflow():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  try:
8
+ # Ensure the script exists
9
+ script_path = "ComfyUI/hr.py"
10
+ if not os.path.exists(script_path):
11
+ return "Error: hr.py not found!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Run the script
14
+ result = subprocess.run(["python", script_path], capture_output=True, text=True)
15
+
16
+ # Return the output
17
+ return result.stdout if result.returncode == 0 else f"Error:\n{result.stderr}"
18
+
19
+ except Exception as e:
20
+ return f"Exception: {str(e)}"
21
+
22
+ # Create a simple UI with a button
23
+ ui = gr.Interface(
24
+ fn=run_workflow,
25
+ inputs=[],
26
+ outputs="text",
27
+ title="Run ComfyUI Workflow",
28
+ description="Click the button to execute hr.py and generate an image."
29
  )
30
 
31
+ # Launch the Gradio app
32
  if __name__ == "__main__":
33
+ ui.launch()