| import gradio as gr |
| import openai |
| import os |
| import datetime |
| from gtts import gTTS |
| from io import BytesIO |
|
|
| |
| openai.api_key = os.getenv("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") |
|
|
| def generate_response(user_input, chat_history): |
| |
| messages = [{"role": "system", "content": "You are VORTEX, an AI chatbot developed by Jarvis-11. Be helpful, knowledgeable, and friendly."}] |
| for msg in chat_history: |
| messages.append({"role": "user", "content": msg[0]}) |
| messages.append({"role": "assistant", "content": msg[1]}) |
|
|
| messages.append({"role": "user", "content": user_input}) |
|
|
| try: |
| response = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=messages |
| ) |
| bot_reply = response['choices'][0]['message']['content'].strip() |
| except Exception as e: |
| bot_reply = f"Error: {str(e)}" |
|
|
| return bot_reply |
|
|
| def chatbot_interface(user_input, chat_history, file, audio_input): |
| if audio_input is not None: |
| import speech_recognition as sr |
| recognizer = sr.Recognizer() |
| with sr.AudioFile(audio_input) as source: |
| audio_data = recognizer.record(source) |
| try: |
| user_input = recognizer.recognize_google(audio_data) |
| except sr.UnknownValueError: |
| user_input = "Sorry, I couldn't understand the audio." |
|
|
| |
| if file is not None: |
| user_input += f"\n(File uploaded: {file.name})" |
|
|
| bot_response = generate_response(user_input, chat_history) |
| chat_history.append((user_input, bot_response)) |
|
|
| |
| tts = gTTS(bot_response) |
| tts_io = BytesIO() |
| tts.write_to_fp(tts_io) |
| tts_io.seek(0) |
|
|
| return chat_history, tts_io |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("# 🤖 VORTEX - Your AI Companion") |
|
|
| chatbot = gr.Chatbot() |
| state = gr.State([]) |
|
|
| with gr.Row(): |
| txt = gr.Textbox(placeholder="Type your message here...", label="Your Message") |
| file_input = gr.File(label="Upload a File (Optional)") |
| audio_input = gr.Audio(type="filepath", label="Upload Voice Message") |
|
|
|
|
| voice_output = gr.Audio(label="VORTEX Voice Reply") |
|
|
| txt.submit(chatbot_interface, [txt, state, file_input, audio_input], [chatbot, voice_output]) |
| file_input.change(chatbot_interface, [txt, state, file_input, audio_input], [chatbot, voice_output]) |
| audio_input.change(chatbot_interface, [txt, state, file_input, audio_input], [chatbot, voice_output]) |
|
|
| gr.Markdown("---") |
| gr.Markdown("Built with ❤️ by Jarvis-11 | Powered by OpenAI & Gradio") |
|
|
| demo.launch() |
|
|