lalaru commited on
Commit
3e2b177
·
verified ·
1 Parent(s): 11af2e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -39
app.py CHANGED
@@ -48,35 +48,37 @@ if not os.path.exists(HISTORY_FILE):
48
 
49
  # === Functions ===
50
  def analyze_text(user_input):
51
- """Send text to Groq API and get corrections + alternatives"""
52
- prompt = f"""
53
- User input: {user_input}
54
- Task:
55
- 1. Correct grammar & spelling.
56
- 2. Provide a formal version.
57
- 3. Provide an informal version.
58
- 4. Highlight difficult/new words.
59
- """
60
-
61
- response = client.chat.completions.create(
62
- model="mixtral-8x7b-32768",
63
- messages=[{"role": "user", "content": prompt}],
64
- )
65
-
66
- corrected_text = response.choices[0].message["content"]
67
-
68
- # Save history
69
- with open(HISTORY_FILE, "r+") as f:
70
- history = json.load(f)
71
- history.append({
72
- "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
73
- "input": user_input,
74
- "response": corrected_text
75
- })
76
- f.seek(0)
77
- json.dump(history, f, indent=2)
78
-
79
- return corrected_text
 
 
80
 
81
 
82
  def speech_to_text(audio_file):
@@ -93,18 +95,14 @@ def speech_to_text(audio_file):
93
 
94
 
95
  def text_to_speech(text):
96
- """Convert text to speech (with fallback)"""
97
- output_file = os.path.join(tempfile.gettempdir(), "output.mp3")
98
-
99
- if TTS_ENGINE == "pyttsx3":
100
- engine = pyttsx3.init()
101
- engine.save_to_file(text, output_file)
102
- engine.runAndWait()
103
- else:
104
  tts = gTTS(text)
105
  tts.save(output_file)
106
-
107
- return output_file
 
108
 
109
 
110
  def get_history():
 
48
 
49
  # === Functions ===
50
  def analyze_text(user_input):
51
+ try:
52
+ prompt = f"""
53
+ User input: {user_input}
54
+ Task:
55
+ 1. Correct grammar & spelling.
56
+ 2. Provide a formal version.
57
+ 3. Provide an informal version.
58
+ 4. Highlight difficult/new words.
59
+ """
60
+
61
+ response = client.chat.completions.create(
62
+ model="mixtral-8x7b-32768",
63
+ messages=[{"role": "user", "content": prompt}],
64
+ )
65
+
66
+ corrected_text = response.choices[0].message.content # ✅ fixed
67
+
68
+ # Save history
69
+ with open(HISTORY_FILE, "r+") as f:
70
+ history = json.load(f)
71
+ history.append({
72
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
73
+ "input": user_input,
74
+ "response": corrected_text
75
+ })
76
+ f.seek(0)
77
+ json.dump(history, f, indent=2)
78
+
79
+ return corrected_text
80
+ except Exception as e:
81
+ return f"❌ Error in analyze_text: {str(e)}"
82
 
83
 
84
  def speech_to_text(audio_file):
 
95
 
96
 
97
  def text_to_speech(text):
98
+ try:
99
+ from gtts import gTTS
100
+ output_file = os.path.join(tempfile.gettempdir(), "output.mp3")
 
 
 
 
 
101
  tts = gTTS(text)
102
  tts.save(output_file)
103
+ return output_file
104
+ except Exception as e:
105
+ return None
106
 
107
 
108
  def get_history():