Spaces:
Sleeping
Sleeping
Samanta Das commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,198 +1,248 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
from groq import Groq
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
-
# Load environment variables
|
| 9 |
-
load_dotenv()
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
"Japanese": "ja",
|
| 32 |
-
"Korean": "ko",
|
| 33 |
-
"Hindi": "hi",
|
| 34 |
-
"Arabic": "ar",
|
| 35 |
-
"Russian": "ru"
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
def translate_text(text, source_lang, target_lang):
|
| 39 |
-
"""Translate text between languages."""
|
| 40 |
-
if source_lang == target_lang:
|
| 41 |
-
return text
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
translator = GoogleTranslator(source=source_lang, target=target_lang)
|
| 45 |
-
return translator.translate(text)
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Translation error: {str(e)}")
|
| 48 |
-
return text
|
| 49 |
-
|
| 50 |
-
def get_gemini_response(question):
|
| 51 |
-
"""Get response from Gemini model."""
|
| 52 |
-
try:
|
| 53 |
-
model = genai.GenerativeModel(
|
| 54 |
-
model_name="gemini-1.5-flash-002",
|
| 55 |
-
generation_config={
|
| 56 |
-
"temperature": 0.7,
|
| 57 |
-
"max_output_tokens": 4096
|
| 58 |
-
}
|
| 59 |
-
)
|
| 60 |
-
response = model.generate_content(question)
|
| 61 |
-
return response.text
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"Gemini error: {str(e)}")
|
| 64 |
-
return None
|
| 65 |
-
|
| 66 |
-
def get_groq_response(question):
|
| 67 |
-
"""Get response from Groq model."""
|
| 68 |
-
try:
|
| 69 |
-
response = groq_client.chat.completions.create(
|
| 70 |
-
model="llama3-70b-8192",
|
| 71 |
-
messages=[
|
| 72 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 73 |
-
{"role": "user", "content": question}
|
| 74 |
-
],
|
| 75 |
-
temperature=0.7,
|
| 76 |
-
max_tokens=4096
|
| 77 |
-
)
|
| 78 |
-
return response.choices[0].message.content
|
| 79 |
-
except Exception as e:
|
| 80 |
-
print(f"Groq error: {str(e)}")
|
| 81 |
-
return None
|
| 82 |
-
|
| 83 |
-
def process_question(question, language, question_type):
|
| 84 |
-
"""Process the question and return responses."""
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
with gr.Row():
|
| 138 |
-
with gr.Column():
|
| 139 |
-
# Input components
|
| 140 |
question_input = gr.Textbox(
|
| 141 |
label="Your Question",
|
| 142 |
placeholder="Type your question here...",
|
| 143 |
lines=5
|
| 144 |
)
|
| 145 |
|
| 146 |
-
with gr.
|
| 147 |
language_input = gr.Dropdown(
|
| 148 |
-
label="Language",
|
| 149 |
-
choices=
|
|
|
|
| 150 |
value="English"
|
| 151 |
)
|
| 152 |
-
|
| 153 |
question_type = gr.Dropdown(
|
| 154 |
label="Question Type",
|
| 155 |
choices=["general", "math", "job"],
|
| 156 |
value="general"
|
| 157 |
)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
label="
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
interactive=False
|
| 178 |
)
|
| 179 |
-
|
| 180 |
-
# Button actions
|
| 181 |
-
submit_btn.click(
|
| 182 |
-
process_question,
|
| 183 |
-
inputs=[question_input, language_input, question_type],
|
| 184 |
-
outputs=[english_question, english_answer, translated_answer]
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
clear_btn.click(
|
| 188 |
-
lambda: ("", "", ""),
|
| 189 |
-
outputs=[english_question, english_answer, translated_answer]
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
return app
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
-
app
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
+
from typing import Tuple, Dict, List
|
| 5 |
from deep_translator import GoogleTranslator
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
+
# Load environment variables - Adapted for Hugging Face
|
| 11 |
+
load_dotenv() # Ensure to call the function to load the environment variables
|
| 12 |
+
|
| 13 |
+
# Multiple Groq API Keys
|
| 14 |
+
GROQ_API_KEYS = [
|
| 15 |
+
os.getenv("GORQ_API_KEY_1"),
|
| 16 |
+
os.getenv("GORQ_API_KEY_2"),
|
| 17 |
+
os.getenv("GORQ_API_KEY_3"),
|
| 18 |
+
os.getenv("GORQ_API_KEY_4"),
|
| 19 |
+
os.getenv("GORQ_API_KEY_5")
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Multiple Gemini API Keys
|
| 23 |
+
GEMINI_API_KEYS = [
|
| 24 |
+
os.getenv("GEMINI_API_KEY_1"),
|
| 25 |
+
os.getenv("GEMINI_API_KEY_2"),
|
| 26 |
+
os.getenv("GEMINI_API_KEY_3"),
|
| 27 |
+
os.getenv("GEMINI_API_KEY_4"),
|
| 28 |
+
os.getenv("GEMINI_API_KEY_5")
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
class TextCleaner:
|
| 32 |
+
"""Handles cleaning and structuring of input text."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
@staticmethod
|
| 35 |
+
def clean_markdown(text: str) -> str:
|
| 36 |
+
"""Remove unnecessary markdown and formatting symbols."""
|
| 37 |
+
# Remove excessive markdown symbols
|
| 38 |
+
text = re.sub(r'#{3,}', '##', text)
|
| 39 |
+
text = re.sub(r'\*{3,}', '**', text)
|
| 40 |
+
text = re.sub(r'_{3,}', '__', text)
|
| 41 |
+
text = re.sub(r'~{3,}', '~~', text)
|
| 42 |
+
text = re.sub(r'\n{3,}', '\n\n', text)
|
| 43 |
+
text = re.sub(r' {2,}', ' ', text)
|
| 44 |
+
text = re.sub(r'`{3,}', '```', text)
|
| 45 |
+
return text.strip()
|
| 46 |
+
|
| 47 |
+
class LanguageManager:
|
| 48 |
+
"""Manages supported languages and their configurations."""
|
| 49 |
|
| 50 |
+
SUPPORTED_LANGUAGES: Dict[str, str] = {
|
| 51 |
+
# Languages mapping...
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
@classmethod
|
| 55 |
+
def get_language_code(cls, language_name: str) -> str:
|
| 56 |
+
return cls.SUPPORTED_LANGUAGES.get(language_name.lower(), "en")
|
| 57 |
+
|
| 58 |
+
@classmethod
|
| 59 |
+
def get_language_name(cls, language_code: str) -> str:
|
| 60 |
+
for name, code in cls.SUPPORTED_LANGUAGES.items():
|
| 61 |
+
if code == language_code:
|
| 62 |
+
return name.replace('_', ' ').title()
|
| 63 |
+
return "English"
|
| 64 |
+
|
| 65 |
+
class ModelManager:
|
| 66 |
+
"""Manages different language models and their responses."""
|
| 67 |
+
|
| 68 |
+
def __init__(self):
|
| 69 |
+
self.model_configs = {
|
| 70 |
+
"math": "llama3-70b-8192",
|
| 71 |
+
"job": "llama-3.2-90b-text-preview",
|
| 72 |
+
"general": "gemini-1.5-flash-002"
|
| 73 |
+
}
|
| 74 |
|
| 75 |
+
# Initialize API clients
|
| 76 |
+
self.groq_client = self._initialize_groq_client()
|
| 77 |
+
self.gemini_client = self._initialize_gemini_client()
|
| 78 |
+
|
| 79 |
+
def _initialize_groq_client(self):
|
| 80 |
+
for key in GROQ_API_KEYS:
|
| 81 |
+
if key: # Check if the key is valid
|
| 82 |
+
return Groq(api_key=key)
|
| 83 |
+
raise ValueError("No valid Groq API keys found.")
|
| 84 |
+
|
| 85 |
+
def _initialize_gemini_client(self):
|
| 86 |
+
for key in GEMINI_API_KEYS:
|
| 87 |
+
if key: # Check if the key is valid
|
| 88 |
+
genai.configure(api_key=key)
|
| 89 |
+
return
|
| 90 |
+
raise ValueError("No valid Gemini API keys found.")
|
| 91 |
+
|
| 92 |
+
def get_model_response(self, question: str, question_type: str) -> str:
|
| 93 |
+
"""Get response from appropriate model with preprocessing."""
|
| 94 |
+
model_name = self.model_configs.get(question_type, self.model_configs["general"])
|
| 95 |
|
| 96 |
+
try:
|
| 97 |
+
if "gemini" in model_name and GEMINI_API_KEYS[0]: # Use the first Gemini key
|
| 98 |
+
return self._get_gemini_response(question)
|
| 99 |
+
elif GROQ_API_KEYS[0]: # Use the first Groq key
|
| 100 |
+
return self._get_groq_response(question, model_name)
|
| 101 |
+
else:
|
| 102 |
+
return "API keys not configured. Please check your environment variables."
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Model error: {str(e)}")
|
| 105 |
+
return "I apologize, but I'm unable to process your request at the moment. Please try again later."
|
| 106 |
+
|
| 107 |
+
def _get_groq_response(self, question: str, model_name: str) -> str:
|
| 108 |
+
"""Get response from Groq model."""
|
| 109 |
+
try:
|
| 110 |
+
response = self.groq_client.chat.completions.create(
|
| 111 |
+
model=model_name,
|
| 112 |
+
messages=[
|
| 113 |
+
{"role": "system", "content": "You are a helpful assistant. For calculations, show step-by-step solutions."},
|
| 114 |
+
{"role": "user", "content": question}
|
| 115 |
+
],
|
| 116 |
+
temperature=0.7,
|
| 117 |
+
max_tokens=4096 # Reduced for better performance
|
| 118 |
+
)
|
| 119 |
+
return TextCleaner.clean_markdown(response.choices[0].message.content)
|
| 120 |
+
except Exception as e:
|
| 121 |
+
raise Exception(f"Groq API error: {str(e)}")
|
| 122 |
+
|
| 123 |
+
def _get_gemini_response(self, question: str) -> str:
|
| 124 |
+
"""Get response from Gemini model."""
|
| 125 |
+
try:
|
| 126 |
+
model = genai.GenerativeModel(
|
| 127 |
+
model_name="gemini-1.5-flash-002",
|
| 128 |
+
generation_config={
|
| 129 |
+
"temperature": 0.7,
|
| 130 |
+
"max_output_tokens": 4096 # Reduced for better performance
|
| 131 |
+
}
|
| 132 |
+
)
|
| 133 |
+
response = model.generate_content(question)
|
| 134 |
+
return TextCleaner.clean_markdown(response.text)
|
| 135 |
+
except Exception as e:
|
| 136 |
+
raise Exception(f"Gemini API error: {str(e)}")
|
| 137 |
+
|
| 138 |
+
class TranslationManager:
|
| 139 |
+
"""Manages translation between different languages."""
|
| 140 |
+
|
| 141 |
+
def __init__(self):
|
| 142 |
+
self._supported_languages = set(LanguageManager.SUPPORTED_LANGUAGES.values())
|
| 143 |
+
self._translation_cache: Dict[str, str] = {}
|
| 144 |
+
|
| 145 |
+
def translate_text(self, text: str, source_lang: str, target_lang: str) -> str:
|
| 146 |
+
"""Translate text between languages with caching and error handling."""
|
| 147 |
+
if source_lang == target_lang:
|
| 148 |
+
return text
|
| 149 |
|
| 150 |
+
cache_key = f"{source_lang}:{target_lang}:{text}"
|
| 151 |
+
if cache_key in self._translation_cache:
|
| 152 |
+
return self._translation_cache[cache_key]
|
| 153 |
|
| 154 |
+
try:
|
| 155 |
+
translator = GoogleTranslator(source=source_lang, target=target_lang)
|
| 156 |
+
translated_text = translator.translate(text)
|
| 157 |
+
self._translation_cache[cache_key] = translated_text
|
| 158 |
+
return translated_text
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"Translation error: {str(e)}")
|
| 161 |
+
return text
|
| 162 |
+
|
| 163 |
+
class QuestifyAI:
|
| 164 |
+
"""Main application class for multilingual question answering."""
|
| 165 |
+
|
| 166 |
+
def __init__(self):
|
| 167 |
+
self.translation_manager = TranslationManager()
|
| 168 |
+
self.model_manager = ModelManager()
|
| 169 |
+
|
| 170 |
+
def process_question(
|
| 171 |
+
self,
|
| 172 |
+
question: str,
|
| 173 |
+
input_language: str,
|
| 174 |
+
question_type: str
|
| 175 |
+
) -> Tuple[str, str, str]:
|
| 176 |
+
"""Process a question through translation and model response pipeline."""
|
| 177 |
+
# Clean input
|
| 178 |
+
question = re.sub(r"[#/*\\]", "", question).strip()
|
| 179 |
|
| 180 |
+
# Get language code
|
| 181 |
+
language_code = LanguageManager.get_language_code(input_language)
|
| 182 |
|
| 183 |
+
# Translate to English if needed
|
| 184 |
+
english_question = (self.translation_manager.translate_text(question, language_code, "en")
|
| 185 |
+
if language_code != "en" else question)
|
| 186 |
|
| 187 |
+
# Get model response
|
| 188 |
+
english_answer = self.model_manager.get_model_response(english_question, question_type)
|
| 189 |
+
|
| 190 |
+
# Translate answer back if needed
|
| 191 |
+
translated_answer = (self.translation_manager.translate_text(english_answer, "en", language_code)
|
| 192 |
+
if language_code != "en" else english_answer)
|
| 193 |
+
|
| 194 |
+
return english_question, english_answer, translated_answer
|
| 195 |
+
|
| 196 |
+
def create_ui(self):
|
| 197 |
+
"""Create Gradio interface for QuestifyAI."""
|
| 198 |
+
with gr.Blocks(title="Questify AI") as app:
|
| 199 |
+
gr.Markdown("# Questify AI - Multilingual Question Answering")
|
| 200 |
+
|
| 201 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
question_input = gr.Textbox(
|
| 203 |
label="Your Question",
|
| 204 |
placeholder="Type your question here...",
|
| 205 |
lines=5
|
| 206 |
)
|
| 207 |
|
| 208 |
+
with gr.Column():
|
| 209 |
language_input = gr.Dropdown(
|
| 210 |
+
label="Select Language",
|
| 211 |
+
choices=sorted([name.replace('_', ' ').title()
|
| 212 |
+
for name in LanguageManager.SUPPORTED_LANGUAGES.keys()]),
|
| 213 |
value="English"
|
| 214 |
)
|
|
|
|
| 215 |
question_type = gr.Dropdown(
|
| 216 |
label="Question Type",
|
| 217 |
choices=["general", "math", "job"],
|
| 218 |
value="general"
|
| 219 |
)
|
| 220 |
+
|
| 221 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 222 |
+
clear_btn = gr.Button("Clear")
|
| 223 |
+
|
| 224 |
+
with gr.Row():
|
| 225 |
+
english_question = gr.Textbox(label="Structured English Question", lines=3)
|
| 226 |
+
english_answer = gr.Textbox(label="Answer in English", lines=5)
|
| 227 |
+
translated_answer = gr.Textbox(label="Translated Answer", lines=5)
|
| 228 |
+
|
| 229 |
+
def handle_submit(question, language, q_type):
|
| 230 |
+
try:
|
| 231 |
+
return self.process_question(question, language, q_type)
|
| 232 |
+
except Exception as e:
|
| 233 |
+
return str(e), "", ""
|
| 234 |
+
|
| 235 |
+
submit_btn.click(
|
| 236 |
+
handle_submit,
|
| 237 |
+
inputs=[question_input, language_input, question_type],
|
| 238 |
+
outputs=[english_question, english_answer, translated_answer]
|
|
|
|
| 239 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
clear_btn.click(lambda: ("", "", ""), outputs=[question_input, english_question, english_answer, translated_answer])
|
| 242 |
+
|
| 243 |
+
return app
|
| 244 |
|
| 245 |
if __name__ == "__main__":
|
| 246 |
+
app = QuestifyAI()
|
| 247 |
+
ui = app.create_ui()
|
| 248 |
+
ui.launch()
|