Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,24 @@ except KeyError:
|
|
15 |
raise ValueError("Please set the GEMINI_API_KEY environment variable.")
|
16 |
client = genai.Client(api_key=api_key)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def generate_ideas(tag):
|
19 |
"""
|
20 |
Generate a diverse set of ideas related to the tag using the LLM.
|
@@ -34,14 +52,16 @@ def generate_ideas(tag):
|
|
34 |
"""
|
35 |
try:
|
36 |
response = client.models.generate_content(
|
37 |
-
model='gemini-2.0-flash',
|
38 |
contents=[prompt],
|
39 |
config=types.GenerateContentConfig(temperature=1.2)
|
40 |
)
|
41 |
print(f"Raw response for ideas: {response.text}") # Debugging
|
42 |
if not response.text or response.text.isspace():
|
43 |
raise ValueError("Empty response from API")
|
44 |
-
|
|
|
|
|
45 |
if 'ideas' not in response_json or not isinstance(response_json['ideas'], list):
|
46 |
raise ValueError("Invalid JSON format: 'ideas' key missing or not a list")
|
47 |
return response_json['ideas']
|
@@ -78,14 +98,16 @@ def generate_item(tag, ideas):
|
|
78 |
"""
|
79 |
try:
|
80 |
response = client.models.generate_content(
|
81 |
-
model='gemini-2.0-flash',
|
82 |
contents=[prompt],
|
83 |
config=types.GenerateContentConfig(temperature=1.2)
|
84 |
)
|
85 |
print(f"Raw response for item: {response.text}") # Debugging
|
86 |
if not response.text or response.text.isspace():
|
87 |
raise ValueError("Empty response from API")
|
88 |
-
|
|
|
|
|
89 |
if 'caption' not in response_json or 'image_prompt' not in response_json:
|
90 |
raise ValueError("Invalid JSON format: 'caption' or 'image_prompt' key missing")
|
91 |
text = response_json['caption']
|
@@ -486,5 +508,5 @@ with gr.Blocks(
|
|
486 |
outputs=[current_tag, current_index, feed_items, feed_html, is_loading]
|
487 |
)
|
488 |
|
489 |
-
# Launch the app
|
490 |
-
demo.launch(
|
|
|
15 |
raise ValueError("Please set the GEMINI_API_KEY environment variable.")
|
16 |
client = genai.Client(api_key=api_key)
|
17 |
|
18 |
+
def clean_response_text(response_text):
|
19 |
+
"""
|
20 |
+
Clean the API response by removing Markdown code block markers.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
response_text (str): The raw response text from the API.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
str: The cleaned response text.
|
27 |
+
"""
|
28 |
+
# Remove ```json and ``` markers
|
29 |
+
cleaned_text = response_text.strip()
|
30 |
+
if cleaned_text.startswith("```json"):
|
31 |
+
cleaned_text = cleaned_text[len("```json"):].strip()
|
32 |
+
if cleaned_text.endswith("```"):
|
33 |
+
cleaned_text = cleaned_text[:-len("```")].strip()
|
34 |
+
return cleaned_text
|
35 |
+
|
36 |
def generate_ideas(tag):
|
37 |
"""
|
38 |
Generate a diverse set of ideas related to the tag using the LLM.
|
|
|
52 |
"""
|
53 |
try:
|
54 |
response = client.models.generate_content(
|
55 |
+
model='gemini-2.0-flash',
|
56 |
contents=[prompt],
|
57 |
config=types.GenerateContentConfig(temperature=1.2)
|
58 |
)
|
59 |
print(f"Raw response for ideas: {response.text}") # Debugging
|
60 |
if not response.text or response.text.isspace():
|
61 |
raise ValueError("Empty response from API")
|
62 |
+
# Clean the response text
|
63 |
+
cleaned_text = clean_response_text(response.text)
|
64 |
+
response_json = json.loads(cleaned_text)
|
65 |
if 'ideas' not in response_json or not isinstance(response_json['ideas'], list):
|
66 |
raise ValueError("Invalid JSON format: 'ideas' key missing or not a list")
|
67 |
return response_json['ideas']
|
|
|
98 |
"""
|
99 |
try:
|
100 |
response = client.models.generate_content(
|
101 |
+
model='gemini-2.0-flash',
|
102 |
contents=[prompt],
|
103 |
config=types.GenerateContentConfig(temperature=1.2)
|
104 |
)
|
105 |
print(f"Raw response for item: {response.text}") # Debugging
|
106 |
if not response.text or response.text.isspace():
|
107 |
raise ValueError("Empty response from API")
|
108 |
+
# Clean the response text
|
109 |
+
cleaned_text = clean_response_text(response.text)
|
110 |
+
response_json = json.loads(cleaned_text)
|
111 |
if 'caption' not in response_json or 'image_prompt' not in response_json:
|
112 |
raise ValueError("Invalid JSON format: 'caption' or 'image_prompt' key missing")
|
113 |
text = response_json['caption']
|
|
|
508 |
outputs=[current_tag, current_index, feed_items, feed_html, is_loading]
|
509 |
)
|
510 |
|
511 |
+
# Launch the app (removed share=True since we're on Hugging Face Spaces)
|
512 |
+
demo.launch()
|