Athspi commited on
Commit
0721898
·
verified ·
1 Parent(s): 348e0cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - Flask Backend with AI URL Shortening System
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
@@ -35,10 +35,11 @@ You are a helpful AI assistant named Athspi. When responding:
35
  [SHORTEN]https://example.com|https://spoo.me/abc123[/SHORTEN]
36
  7. DO NOT add any extra text inside the SHORTEN tags
37
  8. ONLY use this format when you have a shortened URL to provide
 
38
  """
39
 
40
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
41
- model = genai.GenerativeModel('gemini-2.5-flash', system_instruction=system_instruction)
42
 
43
  # In-memory session storage
44
  chat_sessions = {}
@@ -107,18 +108,19 @@ def shorten_url_with_spoo_me(url):
107
 
108
  if response.status_code == 200:
109
  try:
110
- # Parse the JSON response
111
  result = response.json()
112
  # Extract short_url and strip whitespace
113
  short_url = result.get("short_url", "").strip()
114
  if short_url and short_url.startswith('http'):
115
  return short_url
116
- except Exception as json_error:
117
- print("JSON parse error:", str(json_error))
118
- # Fallback for plain text response
119
  text_response = response.text.strip()
120
  if text_response.startswith('http'):
121
  return text_response
 
 
122
 
123
  print(f"spoo.me error: {response.status_code} - {response.text}")
124
  return None
@@ -144,7 +146,6 @@ def chat():
144
 
145
  # Check if user message contains a URL
146
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
147
- ai_response = None
148
  full_text = ""
149
 
150
  if url_match:
@@ -157,10 +158,10 @@ def chat():
157
 
158
  # Send message to AI with instruction to include short link
159
  ai_response = chat_session.send_message(
160
- f"User sent this message: '{user_message}'\n\n"
161
- f"I found a URL: {original_url}\n"
162
- f"Shortened URL: {short_url}\n\n"
163
- f"Respond naturally and include this exact format:\n{short_tag}"
164
  )
165
  full_text = ai_response.text
166
  else:
 
1
+ # app.py - Flask Backend with URL Shortening System
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
 
35
  [SHORTEN]https://example.com|https://spoo.me/abc123[/SHORTEN]
36
  7. DO NOT add any extra text inside the SHORTEN tags
37
  8. ONLY use this format when you have a shortened URL to provide
38
+ 9. If you cannot shorten the URL, do not include the SHORTEN tags
39
  """
40
 
41
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
42
+ model = genai.GenerativeModel('gemini-1.5-pro', system_instruction=system_instruction)
43
 
44
  # In-memory session storage
45
  chat_sessions = {}
 
108
 
109
  if response.status_code == 200:
110
  try:
111
+ # Parse the JSON response - THIS IS THE CORRECT WAY
112
  result = response.json()
113
  # Extract short_url and strip whitespace
114
  short_url = result.get("short_url", "").strip()
115
  if short_url and short_url.startswith('http'):
116
  return short_url
117
+ except Exception:
118
+ # If JSON parsing fails, try plain text response
 
119
  text_response = response.text.strip()
120
  if text_response.startswith('http'):
121
  return text_response
122
+ elif len(text_response) > 3:
123
+ return f"https://spoo.me/{text_response}"
124
 
125
  print(f"spoo.me error: {response.status_code} - {response.text}")
126
  return None
 
146
 
147
  # Check if user message contains a URL
148
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
 
149
  full_text = ""
150
 
151
  if url_match:
 
158
 
159
  # Send message to AI with instruction to include short link
160
  ai_response = chat_session.send_message(
161
+ f"{user_message}\n\n"
162
+ f"I detected a URL in your message: {original_url}\n"
163
+ f"Shortened version: {short_url}\n\n"
164
+ f"Please respond naturally and include this exact format:\n{short_tag}"
165
  )
166
  full_text = ai_response.text
167
  else: