sadegh803211 commited on
Commit
8c83126
·
verified ·
1 Parent(s): 66d58d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -50
app.py CHANGED
@@ -1,6 +1,5 @@
1
  # ==============================================================================
2
  # Part 1: Core Classes from the Original Script
3
- # All the necessary helper classes for the RAG system are defined here.
4
  # ==============================================================================
5
  import os
6
  import re
@@ -13,30 +12,19 @@ import torch
13
  import faiss
14
  from PIL import Image, ImageOps
15
 
16
- # Hugging Face Transformers & Sentence-Transformers
17
- from transformers import (
18
- CLIPVisionModel,
19
- CLIPImageProcessor,
20
- AutoTokenizer,
21
- AutoModel,
22
- )
23
  from sentence_transformers import SentenceTransformer
24
-
25
- # Google Generative AI
26
  import google.generativeai as genai
27
  from google.generativeai.types import GenerationConfig
28
-
29
- # Gradio for Web UI
30
  import gradio as gr
31
 
32
-
33
  # --- CONFIGURATION CLASS ---
34
  class Config:
35
  per_option_ctx: int = 5
36
  max_text_len: int = 512
37
  docstore_path: str = "indexes/docstore.parquet"
38
- glot_model_hf: str = "Arshiaizd/Glot500-FineTuned"
39
- mclip_text_model_hf: str = "Arshiaizd/MCLIP_FA_FineTuned"
40
  clip_vision_model: str = "SajjadAyoubi/clip-fa-vision"
41
  glot_index_out: str = "indexes/I_glot_text_fa.index"
42
  clip_index_out: str = "indexes/I_clip_text_fa.index"
@@ -105,7 +93,7 @@ class BaseRetriever:
105
  if os.path.isfile(self.index_path):
106
  self.index = faiss.read_index(self.index_path)
107
  else:
108
- raise FileNotFoundError(f"Index file not found at {self.index_path}. Make sure it's uploaded to your Space.")
109
  def search(self, query_vec: np.ndarray, k: int) -> List[Tuple[int, float]]:
110
  D, I = self.index.search(query_vec[None, :].astype(np.float32), k)
111
  return list(zip(I[0].tolist(), D[0].tolist()))
@@ -127,7 +115,7 @@ class TextIndexRetriever(BaseRetriever):
127
  class VLM_GenAI:
128
  def __init__(self, api_key: str, model_name: str, temperature: float = 0.1, max_output_tokens: int = 1024):
129
  if not api_key or "YOUR" in api_key:
130
- raise ValueError("Gemini API Key is missing or is a placeholder. Please add it to your Hugging Face Space secrets.")
131
  genai.configure(api_key=api_key)
132
  self.model = genai.GenerativeModel(model_name)
133
  self.generation_config = GenerationConfig(temperature=temperature, max_output_tokens=max_output_tokens)
@@ -150,7 +138,6 @@ class RAGSystem:
150
  # Part 2: Gradio Web Application
151
  # ==============================================================================
152
 
153
- # --- 1. LOAD MODELS AND INDEXES (This runs only once when the app starts) ---
154
  print("Initializing configuration...")
155
  cfg = Config()
156
  print("Loading RAG system...")
@@ -160,7 +147,6 @@ api_key = os.environ.get("GEMINI_API_KEY")
160
  vlm = VLM_GenAI(api_key, model_name="models/gemini-1.5-flash")
161
  print("System ready.")
162
 
163
- # --- 2. DEFINE THE FUNCTION TO HANDLE USER INPUT ---
164
  def run_rag_query(question_text: str, question_image: Optional[Image.Image]) -> Tuple[str, str]:
165
  if not question_text.strip():
166
  return "Please ask a question.", ""
@@ -193,47 +179,29 @@ def run_rag_query(question_text: str, question_image: Optional[Image.Image]) ->
193
 
194
  # --- 3. CREATE THE GRADIO INTERFACE ---
195
 
196
- # Define your custom CSS for the background image
197
  custom_css = """
198
- body {
199
- /* The URL to your background image in the HF Repo */
200
- background-image: url('/file=background/back.jpg');
201
- /* Make the image cover the whole background */
202
- background-size: cover;
203
- /* Don't repeat the image */
204
- background-repeat: no-repeat;
205
- /* Fix the background image so it doesn't scroll with content */
206
- background-attachment: fixed;
207
- /* Center the background image */
208
- background-position: center;
209
- color: white; /* Set default text color to white for readability */
210
- }
211
-
212
- /* Add a semi-transparent overlay to make text more readable */
213
- body::before {
214
- content: "";
215
- position: absolute;
216
- top: 0; left: 0; right: 0; bottom: 0;
217
- background-color: rgba(0, 0, 0, 0.5); /* Black overlay with 50% opacity */
218
- z-index: -1; /* Place it behind the content */
219
  }
220
 
221
- /* Style the main container to have a semi-transparent background */
222
  .gradio-container {
223
- background: rgba(0, 0, 0, 0.6) !important; /* Darker, semi-transparent background for the app area */
224
- border-radius: 20px !important;
225
- border: 1px solid rgba(255, 255, 255, 0.2);
226
  }
227
 
228
- /* Make textboxes semi-transparent */
229
- textarea, input[type="text"] {
230
- background-color: rgba(255, 255, 255, 0.1) !important;
231
  color: white !important;
232
- border: 1px solid rgba(255, 255, 255, 0.3) !important;
233
  }
234
  """
235
 
236
- with gr.Blocks(css=custom_css, title="Persian Culinary RAG") as demo:
237
  gr.Markdown("# 🍲 Persian Culinary RAG Demo")
238
  gr.Markdown("Ask a question about Iranian food, with or without an image, to see the RAG system in action.")
239
 
 
1
  # ==============================================================================
2
  # Part 1: Core Classes from the Original Script
 
3
  # ==============================================================================
4
  import os
5
  import re
 
12
  import faiss
13
  from PIL import Image, ImageOps
14
 
15
+ from transformers import (CLIPVisionModel, CLIPImageProcessor, AutoTokenizer, AutoModel)
 
 
 
 
 
 
16
  from sentence_transformers import SentenceTransformer
 
 
17
  import google.generativeai as genai
18
  from google.generativeai.types import GenerationConfig
 
 
19
  import gradio as gr
20
 
 
21
  # --- CONFIGURATION CLASS ---
22
  class Config:
23
  per_option_ctx: int = 5
24
  max_text_len: int = 512
25
  docstore_path: str = "indexes/docstore.parquet"
26
+ glot_model_hf: str = "Arshiaizd/Glot500-Fine-Tuned"
27
+ mclip_text_model_hf: str = "Arshiaizd/MCLIP_FA-FineTuned"
28
  clip_vision_model: str = "SajjadAyoubi/clip-fa-vision"
29
  glot_index_out: str = "indexes/I_glot_text_fa.index"
30
  clip_index_out: str = "indexes/I_clip_text_fa.index"
 
93
  if os.path.isfile(self.index_path):
94
  self.index = faiss.read_index(self.index_path)
95
  else:
96
+ raise FileNotFoundError(f"Index file not found at {self.index_path}.")
97
  def search(self, query_vec: np.ndarray, k: int) -> List[Tuple[int, float]]:
98
  D, I = self.index.search(query_vec[None, :].astype(np.float32), k)
99
  return list(zip(I[0].tolist(), D[0].tolist()))
 
115
  class VLM_GenAI:
116
  def __init__(self, api_key: str, model_name: str, temperature: float = 0.1, max_output_tokens: int = 1024):
117
  if not api_key or "YOUR" in api_key:
118
+ raise ValueError("Gemini API Key is missing. Add it to Space secrets.")
119
  genai.configure(api_key=api_key)
120
  self.model = genai.GenerativeModel(model_name)
121
  self.generation_config = GenerationConfig(temperature=temperature, max_output_tokens=max_output_tokens)
 
138
  # Part 2: Gradio Web Application
139
  # ==============================================================================
140
 
 
141
  print("Initializing configuration...")
142
  cfg = Config()
143
  print("Loading RAG system...")
 
147
  vlm = VLM_GenAI(api_key, model_name="models/gemini-1.5-flash")
148
  print("System ready.")
149
 
 
150
  def run_rag_query(question_text: str, question_image: Optional[Image.Image]) -> Tuple[str, str]:
151
  if not question_text.strip():
152
  return "Please ask a question.", ""
 
179
 
180
  # --- 3. CREATE THE GRADIO INTERFACE ---
181
 
182
+ # CORRECTED AND IMPROVED CSS
183
  custom_css = """
184
+ /* The #gradio-app selector is more specific and reliable */
185
+ #gradio-app {
186
+ background-image: url('file/background/back.jpg') !important;
187
+ background-size: cover !important;
188
+ background-repeat: no-repeat !important;
189
+ background-attachment: fixed !important;
190
+ background-position: center !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  }
192
 
193
+ /* Add a semi-transparent overlay to the main container for better text readability */
194
  .gradio-container {
195
+ background-color: rgba(0, 0, 0, 0.6) !important;
 
 
196
  }
197
 
198
+ /* Style labels to be white so they are visible */
199
+ .gradio-container .gr-label {
 
200
  color: white !important;
 
201
  }
202
  """
203
 
204
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="Persian Culinary RAG") as demo:
205
  gr.Markdown("# 🍲 Persian Culinary RAG Demo")
206
  gr.Markdown("Ask a question about Iranian food, with or without an image, to see the RAG system in action.")
207