Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ import tempfile
|
|
| 6 |
|
| 7 |
# -------------------- API Configuration --------------------
|
| 8 |
# Set your API keys as environment variables in Hugging Face Spaces settings
|
| 9 |
-
GROQ_API_KEY =
|
| 10 |
-
|
| 11 |
|
| 12 |
# Initialize clients
|
| 13 |
-
client = Groq(api_key=
|
| 14 |
llm = ChatGoogleGenerativeAI(
|
| 15 |
model="gemini-2.0-flash",
|
| 16 |
google_api_key=GOOGLE_API_KEY,
|
|
@@ -32,10 +32,42 @@ def transcribe_audio(audio_path, language="ar"):
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error in transcription: {str(e)}", None
|
| 34 |
|
| 35 |
-
def get_ai_response(text):
|
| 36 |
-
"""Get AI response from Gemini"""
|
| 37 |
try:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return response.content
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error getting AI response: {str(e)}"
|
|
|
|
| 6 |
|
| 7 |
# -------------------- API Configuration --------------------
|
| 8 |
# Set your API keys as environment variables in Hugging Face Spaces settings
|
| 9 |
+
# GROQ_API_KEY = "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"
|
| 10 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyD2DMFgcL0kWTQYhii8wseSHY3BRGWSebk"
|
| 11 |
|
| 12 |
# Initialize clients
|
| 13 |
+
client = Groq(api_key="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")
|
| 14 |
llm = ChatGoogleGenerativeAI(
|
| 15 |
model="gemini-2.0-flash",
|
| 16 |
google_api_key=GOOGLE_API_KEY,
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error in transcription: {str(e)}", None
|
| 34 |
|
| 35 |
+
def get_ai_response(text, detected_language="ar"):
|
| 36 |
+
"""Get AI response from Gemini with language-aware system prompt"""
|
| 37 |
try:
|
| 38 |
+
# Create system prompt based on detected language
|
| 39 |
+
if detected_language == "ar":
|
| 40 |
+
system_prompt = """أنت مساعد ذكي ومفيد. يجب عليك الرد باللغة العربية واستخدام نفس اللهجة التي يستخدمها المستخدم.
|
| 41 |
+
|
| 42 |
+
قواعد مهمة:
|
| 43 |
+
- إذا تحدث المستخدم بالعربية الفصحى، رد بالفصحى
|
| 44 |
+
- إذا تحدث المستخدم بلهجة عامية (مصرية، خليجية، شامية، مغاربية، إلخ)، رد بنفس اللهجة
|
| 45 |
+
- حافظ على نفس الأسلوب والنبرة التي يستخدمها المستخدم
|
| 46 |
+
- كن طبيعياً ومحادثاً ومفيداً
|
| 47 |
+
- إذا لم تتأكد من اللهجة، استخدم العربية الفصحى المبسطة
|
| 48 |
+
|
| 49 |
+
الرد يجب أن يكون مختصراً ومفيداً وواضحاً."""
|
| 50 |
+
else:
|
| 51 |
+
system_prompt = """You are a helpful and intelligent assistant. You must respond in the EXACT same language, dialect, and style that the user uses.
|
| 52 |
+
|
| 53 |
+
Important rules:
|
| 54 |
+
- Match the user's language exactly (English, Spanish, French, etc.)
|
| 55 |
+
- Match their dialect or regional variation (American English, British English, etc.)
|
| 56 |
+
- Match their tone and formality level (casual, formal, slang, etc.)
|
| 57 |
+
- Be natural, conversational, and helpful
|
| 58 |
+
- Keep responses concise and clear
|
| 59 |
+
|
| 60 |
+
Respond in the same language as the user's input."""
|
| 61 |
+
|
| 62 |
+
# Create messages with system prompt
|
| 63 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 64 |
+
|
| 65 |
+
messages = [
|
| 66 |
+
SystemMessage(content=system_prompt),
|
| 67 |
+
HumanMessage(content=text)
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
response = llm.invoke(messages)
|
| 71 |
return response.content
|
| 72 |
except Exception as e:
|
| 73 |
return f"Error getting AI response: {str(e)}"
|