Athspi commited on
Commit
3e26e8b
·
verified ·
1 Parent(s): 9104c9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - Complete AI Chat with Memory + Auto Link Shortening + Audio
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
@@ -33,7 +33,7 @@ You are a helpful AI assistant named Athspi. When responding:
33
  [SHORTEN]original_url|short_url[/SHORTEN]
34
  Example:
35
  I've shortened your link for convenience:
36
- [SHORTEN]https://example.com|https://tinyurl.com/abc123[/SHORTEN]
37
  """
38
 
39
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
@@ -72,15 +72,26 @@ def generate_audio(text):
72
  tts.save(filepath)
73
  return filename
74
 
75
- def shorten_url_with_tinyurl(url):
76
- """Shorten URL using TinyURL public API"""
77
  try:
78
- response = requests.get(f"http://tinyurl.com/api-create.php?url={url}")
79
- if response.status_code == 200 and response.text.strip():
80
- return response.text.strip()
81
- return None
 
 
 
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
- print("TinyURL error:", e)
84
  return None
85
 
86
  @app.route('/chat', methods=['POST'])
@@ -102,16 +113,16 @@ def chat():
102
  response = chat_session.send_message(user_message)
103
  full_text = response.text
104
 
105
- # Auto-detect any URL and shorten it
106
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
107
  if url_match:
108
  original_url = url_match.group(0)
109
- short_url = shorten_url_with_tinyurl(original_url)
110
  if short_url and "[SHORTEN]" not in full_text:
111
  short_tag = f"[SHORTEN]{original_url}|{short_url}[/SHORTEN]"
112
  full_text += f"\n\n{short_tag}"
113
 
114
- # Process response
115
  visible_text, audio_content = process_response(full_text)
116
  html_response = convert_markdown_to_html(visible_text)
117
 
 
1
+ # app.py - Flask Backend with spoo.me URL Shortening + Chat Memory + Audio
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
 
33
  [SHORTEN]original_url|short_url[/SHORTEN]
34
  Example:
35
  I've shortened your link for convenience:
36
+ [SHORTEN]https://example.com|https://spoo.me/abc123[/SHORTEN]
37
  """
38
 
39
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
 
72
  tts.save(filepath)
73
  return filename
74
 
75
+ def shorten_url_with_spoo_me(url):
76
+ """Shorten URL using spoo.me API"""
77
  try:
78
+ payload = {
79
+ "url": url,
80
+ "alias": "" # Leave empty for auto-generated
81
+ }
82
+ headers = {
83
+ "Accept": "application/json",
84
+ "Content-Type": "application/x-www-form-urlencoded"
85
+ }
86
+ response = requests.post("https://spoo.me/", data=payload, headers=headers, timeout=5)
87
+ if response.status_code == 200:
88
+ result = response.json()
89
+ return result.get("shorturl") or result.get("url") # spoo.me returns either
90
+ else:
91
+ print("spoo.me error:", response.status_code, response.text)
92
+ return None
93
  except Exception as e:
94
+ print("Request failed:", str(e))
95
  return None
96
 
97
  @app.route('/chat', methods=['POST'])
 
113
  response = chat_session.send_message(user_message)
114
  full_text = response.text
115
 
116
+ # Auto-detect any URL and shorten it using spoo.me
117
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
118
  if url_match:
119
  original_url = url_match.group(0)
120
+ short_url = shorten_url_with_spoo_me(original_url)
121
  if short_url and "[SHORTEN]" not in full_text:
122
  short_tag = f"[SHORTEN]{original_url}|{short_url}[/SHORTEN]"
123
  full_text += f"\n\n{short_tag}"
124
 
125
+ # Process final response
126
  visible_text, audio_content = process_response(full_text)
127
  html_response = convert_markdown_to_html(visible_text)
128