Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines67891011121314151615 unmodified lines323334353637383940415 unmodified lines# -------------------- API Configuration --------------------# Set your API keys as environment variables in Hugging Face Spaces settingsGROQ_API_KEY = os.environ.get("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", "")GOOGLE_API_KEY = os.environ.get("AIzaSyD2DMFgcL0kWTQYhii8wseSHY3BRGWSebk", "")# Initialize clientsclient = Groq(api_key=GROQ_API_KEY)llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash",google_api_key=GOOGLE_API_KEY,15 unmodified linesexcept Exception as e:return f"Error in transcription: {str(e)}", Nonedef get_ai_response(text):"""Get AI response from Gemini"""try:response = llm.invoke(text)return response.contentexcept Exception as e:return f"Error getting AI response: {str(e)}"5 unmodified lines67891011121314151615 unmodified lines3233343536373839404142434445464748495051525354555657585960616263646566676869707172735 unmodified lines# -------------------- API Configuration --------------------# Set your API keys as environment variables in Hugging Face Spaces settings# GROQ_API_KEY = "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"os.environ["GOOGLE_API_KEY"] = "AIzaSyD2DMFgcL0kWTQYhii8wseSHY3BRGWSebk"# Initialize clientsclient = Groq(api_key="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")llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash",google_api_key=GOOGLE_API_KEY,15 unmodified linesexcept Exception as e:return f"Error in transcription: {str(e)}", Nonedef get_ai_response(text, detected_language="ar"):"""Get AI response from Gemini with language-aware system prompt"""try:# Create system prompt based on detected languageif detected_language == "ar":system_prompt = """أنت مساعد ذكي ومفيد. يجب عليك الرد باللغة العربية واستخدام نفس اللهجة التي يستخدمها المستخدم.قواعد مهمة:- إذا تحدث المستخدم بالعربية الفصحى، رد بالفصحى- إذا تحدث المستخدم بلهجة عامية (مصرية، خليجية، شامية، مغاربية، إلخ)، رد بنفس اللهجة- حافظ على نفس الأسلوب والنبرة التي يستخدمها المستخدم- كن طبيعياً ومحادثاً ومفيداً- إذا لم تتأكد من اللهجة، استخدم العربية الفصحى المبسطةالرد يجب أن يكون مختصراً ومفيداً وواضحاً."""else:system_prompt = """You are a helpful and intelligent assistant. You must respond in the EXACT same language, dialect, and style that the user uses.Important rules:- Match the user's language exactly (English, Spanish, French, etc.)- Match their dialect or regional variation (American English, British English, etc.)- Match their tone and formality level (casual, formal, slang, etc.)- Be natural, conversational, and helpful- Keep responses concise and clearRespond in the same language as the user's input."""# Create messages with system promptfrom langchain_core.messages import HumanMessage, SystemMessagemessages = [SystemMessage(content=system_prompt),HumanMessage(content=text)]response = llm.invoke(messages)return response.contentexcept Exception as e:return f"Error getting AI response: {str(e)}"
app.py+38-6