-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (57 loc) · 2.2 KB
/
main.py
File metadata and controls
70 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import time
from groq import Groq
from gtts import gTTS
groq_client = Groq(api_key="")
def transcribe_audio(filepath):
with open(filepath, "rb") as f:
response = groq_client.audio.transcriptions.create(
model="whisper-large-v3-turbo",
file=f,
)
return response.text
def get_answer(question):
response = groq_client.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": "You are a helpful agriculture chatbot for Indian farmers."},
{"role": "user", "content": "Give a Brief Of Agriculture Seasons in India"},
{"role": "system", "content": "In India, the agricultural season consists of three major seasons: the Kharif (monsoon), the Rabi (winter), and the Zaid (summer)..."},
{"role": "user", "content": question}
]
)
return response.choices[0].message.content
def typing_effect(text, delay=0.03):
for char in text:
print(char, end='', flush=True)
time.sleep(delay)
print() # Newline at end
def text_to_speech(text, filename):
tts = gTTS(text)
output_path = f"{filename}.mp3"
tts.save(output_path)
return output_path
def main():
mode = input("Choose input type ('text' or 'audio'): ").strip().lower()
if mode == 'text':
question = input("Enter your question: ").strip()
elif mode == 'audio':
filepath = input("Enter the path to your audio file: ").strip()
if not os.path.exists(filepath):
print("❌ File not found.")
return
print("🎤 Transcribing audio...")
question = transcribe_audio(filepath)
print(f"📝 Transcribed Text: {question}")
else:
print("❌ Invalid input type. Use 'text' or 'audio'.")
return
print("🤖 Getting response from LLM...")
answer = get_answer(question)
print("\n✅ Answer:")
typing_effect(answer)
print("\n🔊 Converting answer to speech...")
audio_file = text_to_speech(answer, "response_audio")
print(f"🎧 Voice saved to: {audio_file}")
if __name__ == "__main__":
main()