YoussefSharawy91 commited on
Commit
046a0cc
·
verified ·
1 Parent(s): 6c21228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -2,16 +2,29 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
2
  import datetime
3
  import pytz
4
  import yaml
5
- import importlib.util
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
- # Dynamically load the predict function from Studio-Ghibli-LORA-SDXL/app.py.
10
- # Adjust the path as needed so that it correctly points to the app.py file.
11
- spec = importlib.util.spec_from_file_location("studio_ghibli", "./Studio-Ghibli-LORA-SDXL/app.py")
12
- studio_ghibli = importlib.util.module_from_spec(spec)
13
- spec.loader.exec_module(studio_ghibli)
14
- predict = studio_ghibli.predict
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  @tool
17
  def my_custom_tool(arg1: str, arg2: int) -> str:
@@ -63,13 +76,13 @@ def probe_story_preferences(likes: str, dislikes: str, favorite_theme: str) -> s
63
  @tool
64
  def generate_studio_ghibli_image(prompt: str) -> str:
65
  """
66
- Generates a Studio Ghibli style image using the predict function from the Studio-Ghibli-LORA-SDXL app.
67
 
68
  Args:
69
  prompt: The text prompt describing the desired image.
70
 
71
  Returns:
72
- The result from the predict function (e.g., an image URL or base64 string).
73
  """
74
  try:
75
  result = predict(prompt)
@@ -133,14 +146,13 @@ agent = CodeAgent(
133
  prompt_templates=prompt_templates
134
  )
135
 
136
- # Set an initial conversation message with instructions.
137
  initial_opener = (
138
  "Welcome to Ghibili Interactive Tale!\n\n"
139
- "I am here to guide you through a magical adventure inspired by the enchanting world of Studio Ghibli. "
140
- "I will ask you questions about your story preferences, generate vivid narrative scenes accompanied by beautiful images, "
141
- "and provide interactive options to help shape your journey. \n\n"
142
- "To get started, please tell me what kind of adventure you're in the mood for (e.g., whimsical fairy tale, mysterious quest, or serene escape). "
143
- "Let's embark on this enchanting journey together!"
144
  )
145
  agent.conversation = [{"role": "assistant", "content": initial_opener}]
146
 
 
2
  import datetime
3
  import pytz
4
  import yaml
5
+ import requests
6
+ import sys
7
  from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
+ # --- Dynamically load the predict function from the HF space repository ---
11
+ # Use the raw URL (resolve URL) to fetch app.py from the Studio-Ghibli-LORA-SDXL space.
12
+ app_py_url = "https://huggingface.co/spaces/artificialguybr/Studio-Ghibli-LORA-SDXL/resolve/main/app.py"
13
+ response = requests.get(app_py_url)
14
+ if response.status_code == 200:
15
+ code = response.text
16
+ module_namespace = {}
17
+ try:
18
+ exec(code, module_namespace)
19
+ except Exception as e:
20
+ raise Exception(f"Error executing app.py code: {e}")
21
+ predict = module_namespace.get("predict")
22
+ if not predict:
23
+ raise Exception("Predict function not found in app.py")
24
+ else:
25
+ raise Exception("Failed to load app.py from the repository")
26
+
27
+ # --- Define Tools ---
28
 
29
  @tool
30
  def my_custom_tool(arg1: str, arg2: int) -> str:
 
76
  @tool
77
  def generate_studio_ghibli_image(prompt: str) -> str:
78
  """
79
+ Generates a Studio Ghibli style image using the predict function loaded from app.py.
80
 
81
  Args:
82
  prompt: The text prompt describing the desired image.
83
 
84
  Returns:
85
+ The output of the predict function (e.g., an image URL or base64 string).
86
  """
87
  try:
88
  result = predict(prompt)
 
146
  prompt_templates=prompt_templates
147
  )
148
 
149
+ # Pre-populate the conversation with an opener
150
  initial_opener = (
151
  "Welcome to Ghibili Interactive Tale!\n\n"
152
+ "I will guide you through a magical adventure inspired by Studio Ghibli. "
153
+ "I'll ask you about your story preferences, generate vivid scenes with beautiful images, and offer interactive options so you can shape your journey.\n\n"
154
+ "To start, please tell me what kind of adventure you're in the mood for (e.g., whimsical fairy tale, mysterious quest, or serene escape). "
155
+ "Let's begin this enchanting journey together!"
 
156
  )
157
  agent.conversation = [{"role": "assistant", "content": initial_opener}]
158