sarun25 commited on
Commit
5eb84c2
Β·
1 Parent(s): b10119f

Intrigrate Google API

Browse files
Files changed (2) hide show
  1. app.py +23 -46
  2. requirements.txt +2 -1
app.py CHANGED
@@ -11,48 +11,9 @@ Implements the 5-module architecture from the design document:
11
 
12
  import os
13
  import re
14
- import torch
15
  import gradio as gr
16
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
17
-
18
- # ---------------------------------------------------------------------------
19
- # MODULE 4 β€” Generation Module (LLM setup)
20
- # FIX: Correct model ID + read HF_TOKEN from environment (set as Space secret)
21
- # ---------------------------------------------------------------------------
22
- MODEL_ID = "google/gemma-3-4b-it" # smallest Gemma-3 β€” works on free tier
23
- HF_TOKEN = os.environ.get("HF_TOKEN") # set this in Space Settings β†’ Secrets
24
-
25
- _tokenizer = None
26
- _pipe = None
27
-
28
- def get_tokenizer():
29
- """Returns the tokenizer, loading it on first call."""
30
- global _tokenizer
31
- if _tokenizer is None:
32
- _tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
33
- return _tokenizer
34
-
35
- def get_pipeline():
36
- """Returns the generation pipeline, loading model on first call (lazy load)."""
37
- global _pipe
38
- if _pipe is None:
39
- tokenizer = get_tokenizer()
40
- model = AutoModelForCausalLM.from_pretrained(
41
- MODEL_ID,
42
- token=HF_TOKEN,
43
- torch_dtype=torch.bfloat16,
44
- device_map="auto",
45
- )
46
- _pipe = pipeline(
47
- "text-generation",
48
- model=model,
49
- tokenizer=tokenizer,
50
- max_new_tokens=512,
51
- do_sample=False,
52
- )
53
- return _pipe
54
-
55
-
56
  # ---------------------------------------------------------------------------
57
  # MODULE 3 β€” Pre-processing Module (6-Element Framework)
58
  # ---------------------------------------------------------------------------
@@ -166,10 +127,26 @@ def format_for_display(parsed: dict) -> tuple[str, str, str]:
166
  # ---------------------------------------------------------------------------
167
  # MODULE 4 β€” Generation Module (inference call)
168
  # ---------------------------------------------------------------------------
169
- def generate(prompt: str) -> str:
170
- outputs = get_pipeline()(prompt, return_full_text=False)
171
- return outputs[0]["generated_text"]
172
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
  # ---------------------------------------------------------------------------
175
  # MODULE 2 β€” Validation & Flow Management Module
@@ -202,7 +179,7 @@ def validate_and_evaluate(description: str, code: str):
202
 
203
  # --- Generation ---
204
  try:
205
- raw_output = generate(prompt)
206
  except Exception as exc:
207
  return "", "", "", f"❌ Model error: {exc}"
208
 
 
11
 
12
  import os
13
  import re
 
14
  import gradio as gr
15
+ from google import genai
16
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # ---------------------------------------------------------------------------
18
  # MODULE 3 β€” Pre-processing Module (6-Element Framework)
19
  # ---------------------------------------------------------------------------
 
127
  # ---------------------------------------------------------------------------
128
  # MODULE 4 β€” Generation Module (inference call)
129
  # ---------------------------------------------------------------------------
130
+ MODEL_ID= "gemma-4-31b-it" # LLM Model
131
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") # 1. Fetch the API Key from the environment (configured in Settings -> Secrets)
132
+
133
+ def generate_response(prompt):
134
+ # Check if the API Key is provided
135
+ if not GOOGLE_API_KEY:
136
+ return "⚠️ Error: API Key not found! Please configure GOOGLE_API_KEY in the Settings -> Variables and secrets page of your Space."
137
+
138
+ try:
139
+ # 2. Initialise the Client using the new SDK
140
+ client = genai.Client(api_key=GOOGLE_API_KEY)
141
+
142
+ # 3. Send the prompt to Google AI Studio for processing
143
+ response = client.models.generate_content(
144
+ model=MODEL_ID,
145
+ contents=prompt
146
+ )
147
+ return response.text
148
+ except Exception as e:
149
+ return f"❌ An error occurred: {str(e)}"
150
 
151
  # ---------------------------------------------------------------------------
152
  # MODULE 2 β€” Validation & Flow Management Module
 
179
 
180
  # --- Generation ---
181
  try:
182
+ raw_output = generate_response(prompt)
183
  except Exception as exc:
184
  return "", "", "", f"❌ Model error: {exc}"
185
 
requirements.txt CHANGED
@@ -3,4 +3,5 @@ torch>=2.2.0
3
  accelerate>=0.30.0
4
  gradio>=4.31.0
5
  sentencepiece
6
- protobuf
 
 
3
  accelerate>=0.30.0
4
  gradio>=4.31.0
5
  sentencepiece
6
+ protobuf
7
+ google-genai