akazmi commited on
Commit
79ba995
·
verified ·
1 Parent(s): 985e6f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -5,14 +5,16 @@ import cachetools
5
  # Set your OpenAI API key here
6
  openai.api_key = "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" # Replace with your actual API key
7
 
8
- # Cache settings
9
  cache_size = 1000
10
- ttl = 3600 # Cache expiration time in seconds (1 hour)
11
- cache = cachetools.LRUCache(maxsize=cache_size, ttl=ttl)
12
 
13
  def generate_response(message):
14
  if message in cache:
15
- return cache[message]
 
 
 
16
 
17
  try:
18
  response = openai.Completion.create(
@@ -24,6 +26,7 @@ def generate_response(message):
24
  temperature=0.7,
25
  )
26
  cache[message] = response.choices[0].text.strip()
 
27
  return response.choices[0].text.strip()
28
  except Exception as e:
29
  return f"Error: {str(e)}"
@@ -39,4 +42,6 @@ iface = gr.Interface(
39
  )
40
 
41
  # Launch the interface
42
- iface.launch()
 
 
 
5
  # Set your OpenAI API key here
6
  openai.api_key = "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" # Replace with your actual API key
7
 
 
8
  cache_size = 1000
9
+ expire = 3600 # Cache expiration time in seconds (1 hour)
10
+ cache = LRUCache(maxsize=cache_size)
11
 
12
  def generate_response(message):
13
  if message in cache:
14
+ if cache.get(message).__dict__["_expire"] > time.time(): # Check expiration
15
+ return cache[message]
16
+ else:
17
+ del cache[message] # Remove expired entry
18
 
19
  try:
20
  response = openai.Completion.create(
 
26
  temperature=0.7,
27
  )
28
  cache[message] = response.choices[0].text.strip()
29
+ cache.expire(message, expire) # Set expiration time
30
  return response.choices[0].text.strip()
31
  except Exception as e:
32
  return f"Error: {str(e)}"
 
42
  )
43
 
44
  # Launch the interface
45
+ iface.launch()
46
+
47
+ import time # Import time module for expiration check