JOY-OF-SCIENCE/Newtonbotjos
0
1import os2import asyncio3import gradio as gr4import fasttext5import edge_tts6from googletrans import Translator7from groq import Groq8from pathlib import Path9 10# --- Configuration ---11GROQ_API_KEY = os.getenv("GROQ_API_KEY")12client = Groq(api_key=GROQ_API_KEY)13translator = Translator()14 15# Setup Language Detection16FT_MODEL = Path("lid.176.bin")17if not FT_MODEL.exists():18 import urllib.request19 urllib.request.urlretrieve("https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin", FT_MODEL)20model_ft = fasttext.load_model(str(FT_MODEL))21 22# Newton Prompt23SYSTEM_PROMPT = """You are Sir Isaac Newton (1643-1727). 24Answer only questions that relate to your life, your works (Principia, Opticks), or your time period. 25If asked about anything from the 'future' (after 1727) or unrelated topics, 26politely refuse to answer as it is unknown to you."""27 28# Mapping for Newton-like voices in edge-tts29VOICES = {30 "en": "en-GB-RyanNeural", # British voice for Newton31 "ur": "ur-PK-AsadNeural",32 "hi": "hi-IN-MadhurNeural"33}34 35async def generate_speech(text, lang):36 voice = VOICES.get(lang, "en-GB-RyanNeural")37 communicate = edge_tts.Communicate(text, voice)38 output_path = "output.mp3"39 await communicate.save(output_path)40 return output_path41 42def detect_lang(text):43 pred = model_ft.predict(text.replace("\n", " "))[0][0]44 return pred.split("__")[-1]45 46async def chat_with_newton(audio):47 if audio is None: return "Please speak...", None48 49 # 1. Transcription (STT) via Groq Whisper50 with open(audio, "rb") as file:51 transcription = client.audio.transcriptions.create(52 file=(audio, file.read()),53 model="whisper-large-v3",54 )55 user_text = transcription.text56 57 # 2. Language Detection58 lang = detect_lang(user_text) # en, ur, hi59 60 # 3. LLM Answer (Newton Persona)61 response = client.chat.completions.create(62 model="mixtral-8x7b-32768",63 messages=[64 {"role": "system", "content": SYSTEM_PROMPT},65 {"role": "user", "content": user_text}66 ]67 )68 answer_en = response.choices[0].message.content69 70 # 4. Translation / Bilingual Logic71 final_text = answer_en72 tts_lang = "en"73 74 if lang == "ur":75 ans_ur = translator.translate(answer_en, dest='ur').text76 final_text = f"{answer_en}\n\n{ans_ur}"77 tts_lang = "ur"78 elif lang == "hi":79 ans_hi = translator.translate(answer_en, dest='hi').text80 # Keep scientific terms in English for 'Hinglish'81 for word in ["gravity", "calculus", "optics", "physics"]:82 ans_hi = ans_hi.replace(translator.translate(word, dest='hi').text, word)83 final_text = f"{answer_en}\n\n{ans_hi}"84 tts_lang = "hi"85 86 # 5. Speech Generation (TTS)87 audio_output = await generate_speech(final_text, tts_lang)88 89 return final_text, audio_output90 91# --- Gradio UI ---92with gr.Blocks(theme=gr.themes.Soft()) as demo:93 gr.Markdown("# ๐ Sir Isaac Newton Voice Assistant")94 gr.Markdown("Speak through your mic; Newton will answer in your language (English/Urdu/Hinglish).")95 96 with gr.Row():97 input_audio = gr.Audio(label="Speak Here", type="filepath")98 output_text = gr.Textbox(label="Newton's Response")99 100 output_audio = gr.Audio(label="Voice Reply", autoplay=True)101 102 # Process when audio is uploaded/recorded103 input_audio.change(fn=chat_with_newton, inputs=input_audio, outputs=[output_text, output_audio])104 105demo.queue().launch()