Athspi commited on
Commit
83027f0
Β·
verified Β·
1 Parent(s): 51b17f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -19
app.py CHANGED
@@ -23,16 +23,18 @@ CORS(app) # Enable CORS for all routes
23
 
24
  # Configure Gemini with a system instruction
25
  system_instruction_text = """
26
- You are a helpful, friendly, and informative AI assistant named Athspi.
27
- Your goal is to provide clear, concise, and natural-sounding answers to user queries.
28
- When you respond:
29
- - Use clear and simple language.
30
- - Avoid overly complex sentence structures that might be hard to read aloud.
31
- - Keep the user engaged and offer follow-up questions or related topics where appropriate.
32
- - Ensure your responses are suitable for text-to-speech conversion.
33
- - Provide factual and accurate information.
34
- - If the user asks for audio or to speak the response, include πŸ”Š at the start of your response.
35
- - For coding questions, provide well-formatted code blocks with syntax highlighting.
 
 
36
  """
37
 
38
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
@@ -50,9 +52,24 @@ def convert_markdown_to_html(text):
50
 
51
  def detect_audio_request(text):
52
  """Detect if user is requesting audio"""
53
- audio_keywords = ['audio', 'speak', 'say it', 'read aloud', 'hear', 'listen']
 
 
 
54
  return any(keyword in text.lower() for keyword in audio_keywords)
55
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def generate_audio_file(text):
57
  """Generate audio file from text and return filename"""
58
  cleaned_text = re.sub(r'[\*_`#]', '', text)
@@ -81,26 +98,34 @@ def chat():
81
  # Detect if user is requesting audio
82
  audio_requested = detect_audio_request(user_message)
83
 
 
 
 
 
84
  response = model.generate_content(user_message)
85
- plain_text_response = response.text
 
 
 
86
 
87
  # Generate audio if requested
88
  audio_url = None
89
  if audio_requested:
90
- # Add audio indicator if not already present
91
- if not plain_text_response.startswith("πŸ”Š"):
92
- plain_text_response = "πŸ”Š " + plain_text_response
93
-
 
94
  # Generate audio file
95
- audio_filename = generate_audio_file(plain_text_response)
96
  if audio_filename:
97
  audio_url = f"/static/audio/{audio_filename}"
98
 
99
- html_response = convert_markdown_to_html(plain_text_response)
100
 
101
  return jsonify({
102
  "response_html": html_response,
103
- "response_text": plain_text_response,
104
  "audio_url": audio_url
105
  })
106
 
 
23
 
24
  # Configure Gemini with a system instruction
25
  system_instruction_text = """
26
+ You are a friendly, natural-sounding AI assistant named Athspi.
27
+ When responding:
28
+ - Use a warm, conversational tone
29
+ - Never mention technical terms like "audio", "text", or "response"
30
+ - For stories, begin with "Here's your story πŸ‘‡" followed by a friendly intro
31
+ - For explanations, use simple, clear language
32
+ - Format responses for pleasant reading and listening
33
+ - When audio is requested, include story content between special markers as shown:
34
+ [AUDIO_START]
35
+ [story content here]
36
+ [AUDIO_END]
37
+ But DO NOT include these markers in the visible response
38
  """
39
 
40
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
 
52
 
53
  def detect_audio_request(text):
54
  """Detect if user is requesting audio"""
55
+ audio_keywords = [
56
+ 'audio', 'speak', 'say it', 'read aloud',
57
+ 'hear', 'listen', 'tell me out loud'
58
+ ]
59
  return any(keyword in text.lower() for keyword in audio_keywords)
60
 
61
+ def extract_audio_content(full_text):
62
+ """Extract audio-specific content between markers"""
63
+ pattern = r'\[AUDIO_START\](.*?)\[AUDIO_END\]'
64
+ match = re.search(pattern, full_text, re.DOTALL)
65
+ if match:
66
+ return match.group(1).strip()
67
+ return full_text
68
+
69
+ def clean_visible_response(full_text):
70
+ """Remove audio markers from visible response"""
71
+ return re.sub(r'\[AUDIO_(START|END)\]', '', full_text).strip()
72
+
73
  def generate_audio_file(text):
74
  """Generate audio file from text and return filename"""
75
  cleaned_text = re.sub(r'[\*_`#]', '', text)
 
98
  # Detect if user is requesting audio
99
  audio_requested = detect_audio_request(user_message)
100
 
101
+ # Add instruction for audio markers if requested
102
+ if audio_requested:
103
+ user_message += "\n\nPlease include [AUDIO_START] and [AUDIO_END] markers around the story content."
104
+
105
  response = model.generate_content(user_message)
106
+ full_response = response.text
107
+
108
+ # Clean visible response by removing audio markers
109
+ visible_response = clean_visible_response(full_response)
110
 
111
  # Generate audio if requested
112
  audio_url = None
113
  if audio_requested:
114
+ # Extract audio-specific content
115
+ audio_content = extract_audio_content(full_response)
116
+ if not audio_content:
117
+ audio_content = visible_response
118
+
119
  # Generate audio file
120
+ audio_filename = generate_audio_file(audio_content)
121
  if audio_filename:
122
  audio_url = f"/static/audio/{audio_filename}"
123
 
124
+ html_response = convert_markdown_to_html(visible_response)
125
 
126
  return jsonify({
127
  "response_html": html_response,
128
+ "response_text": visible_response,
129
  "audio_url": audio_url
130
  })
131