Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,29 +7,26 @@ from googleapiclient.discovery import build
|
|
7 |
import gradio as gr
|
8 |
import torch
|
9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Google Calendar API setup with Service Account
|
12 |
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
13 |
# Calendar ID - use your calendar ID here
|
14 |
CALENDAR_ID = os.getenv('CALENDAR_ID', '26f5856049fab3d6648a2f1dea57c70370de6bc1629a5182be1511b0e75d11d3@group.calendar.google.com')
|
15 |
-
# Path to your service account key file
|
16 |
-
SERVICE_ACCOUNT_FILE = os.getenv('SERVICE_ACCOUNT_FILE', 'service-account-key.json')
|
17 |
|
18 |
# Load Llama 3.1 model
|
19 |
MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
20 |
|
21 |
def get_calendar_service():
|
22 |
"""Set up Google Calendar service using service account"""
|
23 |
-
# Load service account info from environment
|
24 |
-
|
25 |
-
|
26 |
-
service_account_info =
|
27 |
-
credentials = service_account.Credentials.from_service_account_info(
|
28 |
-
service_account_info, scopes=SCOPES)
|
29 |
-
else:
|
30 |
-
# For local development, load from file
|
31 |
-
credentials = service_account.Credentials.from_service_account_file(
|
32 |
-
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
33 |
|
34 |
service = build('calendar', 'v3', credentials=credentials)
|
35 |
return service
|
@@ -106,26 +103,6 @@ def add_event_to_calendar(name, date, time_str, duration_minutes=60):
|
|
106 |
print(f"Event details: {json.dumps(event, indent=2)}")
|
107 |
raise
|
108 |
|
109 |
-
# Load model on startup to avoid loading it for each request
|
110 |
-
@gr.utils.memoize(utils=["torch"])
|
111 |
-
def load_llama_model():
|
112 |
-
"""Load the Llama 3.1 model"""
|
113 |
-
print("Loading Llama 3.1 model...")
|
114 |
-
|
115 |
-
# Spaces will handle the quantization, so we use default loading
|
116 |
-
# or you can adjust quantization based on available resources
|
117 |
-
model = AutoModelForCausalLM.from_pretrained(
|
118 |
-
MODEL_ID,
|
119 |
-
torch_dtype=torch.bfloat16,
|
120 |
-
device_map="auto",
|
121 |
-
low_cpu_mem_usage=True,
|
122 |
-
use_cache=True
|
123 |
-
)
|
124 |
-
|
125 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
126 |
-
|
127 |
-
return model, tokenizer
|
128 |
-
|
129 |
def extract_function_call(text):
|
130 |
"""Extract function call parameters from Llama's response text"""
|
131 |
# Look for JSON-like structure in the response
|
@@ -257,56 +234,64 @@ any links to view their appointment.
|
|
257 |
IMPORTANT: Make sure to interpret times correctly. If a user says '2 PM' or just '2',
|
258 |
this likely means 2:00 PM (14:00) in 24-hour format."""
|
259 |
|
260 |
-
# Initialize model and
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
-
#
|
264 |
-
|
265 |
-
"text-generation",
|
266 |
-
model=model,
|
267 |
-
tokenizer=tokenizer,
|
268 |
-
return_full_text=True
|
269 |
-
)
|
270 |
|
271 |
# Create Gradio interface
|
272 |
-
|
273 |
-
#
|
274 |
-
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
-
#
|
281 |
-
|
282 |
-
msg = gr.Textbox(placeholder="Type your message here...", label="Message")
|
283 |
-
clear = gr.Button("Clear Chat")
|
284 |
|
285 |
-
#
|
286 |
-
|
287 |
|
288 |
-
|
289 |
-
def user_input(message, history, conv_history):
|
290 |
-
if message.strip() == "":
|
291 |
-
return "", history, conv_history
|
292 |
-
|
293 |
-
# Get response from Llama
|
294 |
-
response, updated_conv_history = process_with_llama(message, conv_history, llm_pipeline)
|
295 |
-
|
296 |
-
# Update chat display
|
297 |
-
history.append((message, response))
|
298 |
-
|
299 |
-
return "", history, updated_conv_history
|
300 |
-
|
301 |
-
# Connect components
|
302 |
-
msg.submit(user_input, [msg, chatbot, state], [msg, chatbot, state])
|
303 |
-
clear.click(lambda: ([], [{"role": "system", "content": system_prompt}]), None, [chatbot, state])
|
304 |
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
app = create_interface()
|
309 |
|
310 |
-
# Launch
|
311 |
if __name__ == "__main__":
|
312 |
-
|
|
|
7 |
import gradio as gr
|
8 |
import torch
|
9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
10 |
+
from huggingface_hub import login
|
11 |
+
|
12 |
+
# Login to Hugging Face if token is provided (for accessing gated models)
|
13 |
+
if os.getenv("HF_TOKEN"):
|
14 |
+
login(os.getenv("HF_TOKEN"))
|
15 |
|
16 |
# Google Calendar API setup with Service Account
|
17 |
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
18 |
# Calendar ID - use your calendar ID here
|
19 |
CALENDAR_ID = os.getenv('CALENDAR_ID', '26f5856049fab3d6648a2f1dea57c70370de6bc1629a5182be1511b0e75d11d3@group.calendar.google.com')
|
|
|
|
|
20 |
|
21 |
# Load Llama 3.1 model
|
22 |
MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
23 |
|
24 |
def get_calendar_service():
|
25 |
"""Set up Google Calendar service using service account"""
|
26 |
+
# Load service account info from environment
|
27 |
+
service_account_info = json.loads(os.getenv('SERVICE_ACCOUNT_INFO', '{}'))
|
28 |
+
credentials = service_account.Credentials.from_service_account_info(
|
29 |
+
service_account_info, scopes=SCOPES)
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
service = build('calendar', 'v3', credentials=credentials)
|
32 |
return service
|
|
|
103 |
print(f"Event details: {json.dumps(event, indent=2)}")
|
104 |
raise
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
def extract_function_call(text):
|
107 |
"""Extract function call parameters from Llama's response text"""
|
108 |
# Look for JSON-like structure in the response
|
|
|
234 |
IMPORTANT: Make sure to interpret times correctly. If a user says '2 PM' or just '2',
|
235 |
this likely means 2:00 PM (14:00) in 24-hour format."""
|
236 |
|
237 |
+
# Initialize model and pipeline
|
238 |
+
def load_model_and_pipeline():
|
239 |
+
model = AutoModelForCausalLM.from_pretrained(
|
240 |
+
MODEL_ID,
|
241 |
+
torch_dtype=torch.bfloat16,
|
242 |
+
device_map="auto",
|
243 |
+
low_cpu_mem_usage=True
|
244 |
+
)
|
245 |
+
|
246 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
247 |
+
|
248 |
+
# Create text generation pipeline
|
249 |
+
llm_pipeline = pipeline(
|
250 |
+
"text-generation",
|
251 |
+
model=model,
|
252 |
+
tokenizer=tokenizer,
|
253 |
+
return_full_text=True,
|
254 |
+
max_new_tokens=1024
|
255 |
+
)
|
256 |
+
|
257 |
+
return llm_pipeline
|
258 |
+
|
259 |
+
# Initialize conversation history with system prompt
|
260 |
+
conversation_history = [{"role": "system", "content": system_prompt}]
|
261 |
|
262 |
+
# Load model and pipeline at startup
|
263 |
+
llm_pipe = load_model_and_pipeline()
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
# Create Gradio interface
|
266 |
+
with gr.Blocks(title="Calendar Booking Assistant") as demo:
|
267 |
+
gr.Markdown("# Indian Time Zone Appointment Booking with Llama 3.1")
|
268 |
+
gr.Markdown("Say something like 'Book an appointment for John on May 10th at 2pm'")
|
269 |
|
270 |
+
# Chat interface
|
271 |
+
chatbot = gr.Chatbot()
|
272 |
+
msg = gr.Textbox(placeholder="Type your message here...", label="Message")
|
273 |
+
clear = gr.Button("Clear Chat")
|
274 |
+
|
275 |
+
# State for conversation history
|
276 |
+
state = gr.State(conversation_history)
|
277 |
+
|
278 |
+
# Handle user input
|
279 |
+
def user_input(message, history, conv_history):
|
280 |
+
if message.strip() == "":
|
281 |
+
return "", history, conv_history
|
282 |
|
283 |
+
# Get response from Llama
|
284 |
+
response, updated_conv_history = process_with_llama(message, conv_history, llm_pipe)
|
|
|
|
|
285 |
|
286 |
+
# Update chat display
|
287 |
+
history.append((message, response))
|
288 |
|
289 |
+
return "", history, updated_conv_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
|
291 |
+
# Connect components
|
292 |
+
msg.submit(user_input, [msg, chatbot, state], [msg, chatbot, state])
|
293 |
+
clear.click(lambda: ([], [{"role": "system", "content": system_prompt}]), None, [chatbot, state])
|
|
|
294 |
|
295 |
+
# Launch the app
|
296 |
if __name__ == "__main__":
|
297 |
+
demo.launch()
|