Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,33 +48,55 @@ from dateutil.parser import parse
|
|
48 |
# Local modules
|
49 |
from footer import footer
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
54 |
-
temperature=0.7,
|
55 |
-
max_tokens=1024,
|
56 |
-
together_api_key=os.getenv('TOGETHER_API_KEY')
|
57 |
-
)
|
58 |
-
|
59 |
|
60 |
-
# Load
|
61 |
load_dotenv()
|
62 |
SPEECHMATICS_API_KEY = os.getenv('SPEECHMATICS_API_KEY')
|
63 |
-
|
64 |
-
|
|
|
65 |
st.error("Please set TOGETHER_API_KEY environment variable")
|
66 |
st.stop()
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
|
|
|
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
st.error(f"Firebase initialization error: {str(e)}")
|
71 |
|
72 |
initialize_firebase()
|
73 |
|
74 |
-
|
75 |
-
# ----------------- Streamlit Config -------------------
|
76 |
-
st.set_page_config(page_title="ApnaLawyer", layout="centered")
|
77 |
-
|
78 |
col1, col2, col3 = st.columns([1, 30, 1])
|
79 |
with col2:
|
80 |
st.image("images/banner.jpg", use_container_width=True)
|
@@ -86,47 +108,28 @@ st.markdown("""
|
|
86 |
</style>
|
87 |
""", unsafe_allow_html=True)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# Function to translate text
|
94 |
-
def translate_text(text, target_language):
|
95 |
-
try:
|
96 |
-
# Use GoogleTranslator from deep-translator
|
97 |
-
translated_text = GoogleTranslator(source='auto', target=target_language).translate(text)
|
98 |
-
return translated_text
|
99 |
-
except Exception as e:
|
100 |
-
return f"⚠ Translation failed: {str(e)}"
|
101 |
-
|
102 |
-
|
103 |
supported_languages = {
|
104 |
"hindi": "hi",
|
105 |
"english": "en",
|
106 |
-
"hinglish": "hi"
|
107 |
}
|
108 |
|
109 |
devanagari_regex = re.compile(r'[\u0900-\u097F]+')
|
110 |
|
111 |
-
|
112 |
def detect_target_language(prompt):
|
113 |
-
"""Detect if the response should be in Hindi, Hinglish, or English only."""
|
114 |
prompt_lower = prompt.lower().strip()
|
115 |
|
116 |
-
#
|
117 |
-
if re.search(r'[\u0C80-\u0CFF]', prompt): # Kannada unicode block
|
118 |
return "hindi"
|
119 |
|
120 |
-
# Check if explicitly mentioned like: 'in hindi'
|
121 |
for lang_name, lang_code in supported_languages.items():
|
122 |
if f"in {lang_name}" in prompt_lower:
|
123 |
return lang_name
|
124 |
|
125 |
-
# If it contains Devanagari, assume Hindi
|
126 |
if devanagari_regex.search(prompt):
|
127 |
return "hindi"
|
128 |
|
129 |
-
# Detect Hinglish by common Hindi terms in Latin script
|
130 |
if re.search(r'\bdhara\b|\bkanoon\b|\bnyay\b', prompt_lower) and detect(prompt) == 'en':
|
131 |
return "hinglish"
|
132 |
|
@@ -140,37 +143,11 @@ def detect_target_language(prompt):
|
|
140 |
|
141 |
return "english"
|
142 |
|
143 |
-
|
144 |
-
# Initialize the translator
|
145 |
-
#translator = Translator()
|
146 |
-
|
147 |
-
# ----------------- Firebase Init (Pyrebase) -------------------
|
148 |
-
firebase_config = {
|
149 |
-
"apiKey": os.environ["FIREBASE_API_KEY"],
|
150 |
-
"authDomain": os.environ["FIREBASE_AUTH_DOMAIN"],
|
151 |
-
"databaseURL": os.environ["FIREBASE_DATABASE_URL"], # Updated to match your variable
|
152 |
-
"projectId": os.environ["FIREBASE_PROJECT_ID"],
|
153 |
-
"storageBucket": os.environ["FIREBASE_STORAGE_BUCKET"],
|
154 |
-
"messagingSenderId": os.environ["FIREBASE_MESSAGING_SENDER_ID"],
|
155 |
-
"appId": os.environ["FIREBASE_APP_ID"]
|
156 |
-
}
|
157 |
-
|
158 |
-
firebase = pyrebase.initialize_app(firebase_config)
|
159 |
-
auth = firebase.auth()
|
160 |
-
|
161 |
-
# ----------------- Firebase Admin SDK Init -------------------
|
162 |
-
def initialize_firebase():
|
163 |
try:
|
164 |
-
|
165 |
-
# Load JSON from environment variable
|
166 |
-
creds_dict = json.loads(os.environ["FIREBASE_CREDS_JSON"])
|
167 |
-
cred = credentials.Certificate(creds_dict)
|
168 |
-
firebase_app = initialize_app(cred, {
|
169 |
-
'databaseURL': os.environ["FIREBASE_DB_URL"]
|
170 |
-
})
|
171 |
except Exception as e:
|
172 |
-
|
173 |
-
|
174 |
|
175 |
# ----------------- Login/Signup Interface -------------------
|
176 |
import json
|
|
|
48 |
# Local modules
|
49 |
from footer import footer
|
50 |
|
51 |
+
# ----------------- Streamlit Config -------------------
|
52 |
+
st.set_page_config(page_title="ApnaLawyer", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
# ----------------- Load Environment Variables -------------------
|
55 |
load_dotenv()
|
56 |
SPEECHMATICS_API_KEY = os.getenv('SPEECHMATICS_API_KEY')
|
57 |
+
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
|
58 |
+
|
59 |
+
if not TOGETHER_API_KEY:
|
60 |
st.error("Please set TOGETHER_API_KEY environment variable")
|
61 |
st.stop()
|
62 |
|
63 |
+
# ----------------- LLM Init -------------------
|
64 |
+
llm = Together(
|
65 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
66 |
+
temperature=0.7,
|
67 |
+
max_tokens=1024,
|
68 |
+
together_api_key=TOGETHER_API_KEY
|
69 |
+
)
|
70 |
+
|
71 |
+
# ----------------- Firebase Init (Pyrebase) -------------------
|
72 |
+
firebase_config = {
|
73 |
+
"apiKey": os.environ["FIREBASE_API_KEY"],
|
74 |
+
"authDomain": os.environ["FIREBASE_AUTH_DOMAIN"],
|
75 |
+
"databaseURL": os.environ["FIREBASE_DATABASE_URL"],
|
76 |
+
"projectId": os.environ["FIREBASE_PROJECT_ID"],
|
77 |
+
"storageBucket": os.environ["FIREBASE_STORAGE_BUCKET"],
|
78 |
+
"messagingSenderId": os.environ["FIREBASE_MESSAGING_SENDER_ID"],
|
79 |
+
"appId": os.environ["FIREBASE_APP_ID"]
|
80 |
+
}
|
81 |
|
82 |
+
firebase = pyrebase.initialize_app(firebase_config)
|
83 |
+
auth = firebase.auth()
|
84 |
|
85 |
+
# ----------------- Firebase Admin SDK Init -------------------
|
86 |
+
def initialize_firebase():
|
87 |
+
try:
|
88 |
+
if not firebase_admin._apps:
|
89 |
+
creds_dict = json.loads(os.environ["FIREBASE_CREDS_JSON"])
|
90 |
+
cred = credentials.Certificate(creds_dict)
|
91 |
+
firebase_app = initialize_app(cred, {
|
92 |
+
'databaseURL': os.environ["FIREBASE_DB_URL"]
|
93 |
+
})
|
94 |
+
except Exception as e:
|
95 |
st.error(f"Firebase initialization error: {str(e)}")
|
96 |
|
97 |
initialize_firebase()
|
98 |
|
99 |
+
# ----------------- UI -------------------
|
|
|
|
|
|
|
100 |
col1, col2, col3 = st.columns([1, 30, 1])
|
101 |
with col2:
|
102 |
st.image("images/banner.jpg", use_container_width=True)
|
|
|
108 |
</style>
|
109 |
""", unsafe_allow_html=True)
|
110 |
|
111 |
+
# ----------------- Translation Logic -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
supported_languages = {
|
113 |
"hindi": "hi",
|
114 |
"english": "en",
|
115 |
+
"hinglish": "hi"
|
116 |
}
|
117 |
|
118 |
devanagari_regex = re.compile(r'[\u0900-\u097F]+')
|
119 |
|
|
|
120 |
def detect_target_language(prompt):
|
|
|
121 |
prompt_lower = prompt.lower().strip()
|
122 |
|
123 |
+
if re.search(r'[\u0C80-\u0CFF]', prompt): # Kannada block
|
|
|
124 |
return "hindi"
|
125 |
|
|
|
126 |
for lang_name, lang_code in supported_languages.items():
|
127 |
if f"in {lang_name}" in prompt_lower:
|
128 |
return lang_name
|
129 |
|
|
|
130 |
if devanagari_regex.search(prompt):
|
131 |
return "hindi"
|
132 |
|
|
|
133 |
if re.search(r'\bdhara\b|\bkanoon\b|\bnyay\b', prompt_lower) and detect(prompt) == 'en':
|
134 |
return "hinglish"
|
135 |
|
|
|
143 |
|
144 |
return "english"
|
145 |
|
146 |
+
def translate_text(text, target_language):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
try:
|
148 |
+
return GoogleTranslator(source='auto', target=target_language).translate(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
except Exception as e:
|
150 |
+
return f"⚠ Translation failed: {str(e)}"
|
|
|
151 |
|
152 |
# ----------------- Login/Signup Interface -------------------
|
153 |
import json
|