Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| import os | |
| import json | |
| from TTS.api import TTS | |
| from langchain.vectorstores import FAISS | |
| from langchain.embeddings import HuggingFaceEmbeddings | |
| try: | |
| from langchain.vectorstores import FAISS | |
| print("✅ LangChain loaded successfully.") | |
| except Exception as e: | |
| print(f"❌ LangChain failed: {str(e)}") | |
| # Load TTS model | |
| tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False) | |
| # Memory functions | |
| def save_user_prefs(user_id, prefs): | |
| with open(f"{user_id}_prefs.json", "w") as f: | |
| json.dump(prefs, f) | |
| def load_user_prefs(user_id): | |
| try: | |
| with open(f"{user_id}_prefs.json", "r") as f: | |
| return json.load(f) | |
| except FileNotFoundError: | |
| return {} | |
| # RAG context | |
| def retrieve_context(query): | |
| db = FAISS.load_local("rag_vectorstore", HuggingFaceEmbeddings()) | |
| docs = db.similarity_search(query) | |
| return "\n".join([doc.page_content for doc in docs]) | |
| # TTS wrapper | |
| def speak_response(text): | |
| tts.tts_to_file(text=text, file_path="response.wav") | |
| return "response.wav" | |
| # Main response logic | |
| def generate_response(message, request: gr.Request): | |
| token = request.headers.get("Authorization") | |
| if not token: | |
| return "⚠️ You must be signed in to use Hermes.", None | |
| try: | |
| hermes_model = gr.load( | |
| "models/NousResearch/Hermes-4-70B", | |
| provider="nebius", | |
| token=token | |
| ) | |
| except Exception as e: | |
| return f"🚫 Model load failed: {str(e)}", None | |
| try: | |
| reply = hermes_model(message) | |
| audio_path = speak_response(reply) | |
| return reply, audio_path | |
| except Exception as e: | |
| return f"❌ Hermes failed to respond: {str(e)}", None | |
| # File dropdown updater | |
| def update_file_list(file): | |
| return [os.path.basename(file.name)] | |
| # UI | |
| with gr.Blocks(title="Hermes Chat Interface") as demo: | |
| gr.Markdown("## 🧠 Hermes Chat Interface\nSpeak to the solar bard. Login required.") | |
| with gr.Row(): | |
| file_upload = gr.File( | |
| label="Upload Files", | |
| file_types=[".txt", ".pdf", ".py", ".doc", ".md", ".png", ".jpg"], | |
| type="filepath", | |
| interactive=True | |
| ) | |
| file_selector = gr.Dropdown( | |
| label="Select Uploaded File", | |
| choices=[], | |
| interactive=True | |
| ) | |
| file_upload.change(fn=update_file_list, inputs=[file_upload], outputs=[file_selector]) | |
| with gr.Row(): | |
| with gr.Column(): | |
| user_input = gr.Textbox(label="Your question") | |
| submit_btn = gr.Button("Ask Hermes") | |
| with gr.Column(): | |
| response_text = gr.Textbox(label="Hermes says") | |
| response_audio = gr.Audio(label="Voice", type="filepath") | |
| with gr.Accordion("⚙️ Settings", open=False): | |
| model_selector = gr.Dropdown( | |
| label="Choose Model", | |
| choices=["Hermes", "Qwen", "OSS GPT", "DeepSeek"], | |
| value="Hermes" | |
| ) | |
| voice_pitch = gr.Slider(label="Voice Pitch", minimum=0.5, maximum=2.0, value=1.0) | |
| voice_rate = gr.Slider(label="Voice Speed", minimum=0.5, maximum=2.0, value=1.0) | |
| voice_engine = gr.Radio( | |
| label="Voice Engine", | |
| choices=["Local TTS", "Hugging Face TTS", "Google TTS", "Microsoft TTS"], | |
| value="Local TTS" | |
| ) | |
| gemini_key = gr.Textbox(label="Gemini Pro/Flash API Key", type="password") | |
| rag_toggle = gr.Checkbox(label="Enable RAG (Retrieval-Augmented Generation)", value=False) | |
| submit_btn.click( | |
| fn=generate_response, | |
| inputs=[user_input], | |
| outputs=[response_text, response_audio] | |
| ) | |
| demo.launch(ssr_mode=False) | |