Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
-
# app.py (
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
-
from openai import OpenAI # Import the new client
|
5 |
import threading
|
6 |
import time
|
7 |
import numpy as np
|
@@ -10,20 +9,28 @@ import os
|
|
10 |
import pickle
|
11 |
from datetime import datetime
|
12 |
|
13 |
-
# Initialize OpenAI client
|
14 |
-
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
15 |
-
|
16 |
# === CONFIG ===
|
17 |
EMBEDDING_MODEL = "text-embedding-3-small"
|
18 |
CHAT_MODEL = "gpt-4o" # Updated to current model
|
19 |
MEMORY_FILE = "memory.pkl"
|
20 |
INDEX_FILE = "memory.index"
|
21 |
|
|
|
|
|
|
|
22 |
# === EMBEDDING UTILS ===
|
23 |
def get_embedding(text, model=EMBEDDING_MODEL):
|
24 |
text = text.replace("\n", " ")
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def cosine_similarity(vec1, vec2):
|
29 |
vec1 = np.array(vec1)
|
@@ -51,20 +58,32 @@ conversation = []
|
|
51 |
turn_count = 0
|
52 |
auto_mode = False
|
53 |
|
54 |
-
# === CHAT COMPLETION (
|
55 |
def chat_completion(system, messages, model=CHAT_MODEL):
|
56 |
try:
|
57 |
# Build message list with system prompt
|
58 |
full_messages = [{"role": "system", "content": system}]
|
59 |
full_messages.extend(messages)
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
except Exception as e:
|
69 |
return f"[API Error: {str(e)}]"
|
70 |
|
|
|
1 |
+
# app.py (Compatible with OpenAI v0.x and v1.x)
|
2 |
import gradio as gr
|
3 |
import openai
|
|
|
4 |
import threading
|
5 |
import time
|
6 |
import numpy as np
|
|
|
9 |
import pickle
|
10 |
from datetime import datetime
|
11 |
|
|
|
|
|
|
|
12 |
# === CONFIG ===
|
13 |
EMBEDDING_MODEL = "text-embedding-3-small"
|
14 |
CHAT_MODEL = "gpt-4o" # Updated to current model
|
15 |
MEMORY_FILE = "memory.pkl"
|
16 |
INDEX_FILE = "memory.index"
|
17 |
|
18 |
+
# Initialize OpenAI API
|
19 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
20 |
+
|
21 |
# === EMBEDDING UTILS ===
|
22 |
def get_embedding(text, model=EMBEDDING_MODEL):
|
23 |
text = text.replace("\n", " ")
|
24 |
+
|
25 |
+
# Compatible API call for both v0.x and v1.x
|
26 |
+
try:
|
27 |
+
# Try v1.x API first
|
28 |
+
response = openai.embeddings.create(input=[text], model=model)
|
29 |
+
return response.data[0].embedding
|
30 |
+
except AttributeError:
|
31 |
+
# Fallback to v0.x API
|
32 |
+
response = openai.Embedding.create(input=[text], model=model)
|
33 |
+
return response['data'][0]['embedding']
|
34 |
|
35 |
def cosine_similarity(vec1, vec2):
|
36 |
vec1 = np.array(vec1)
|
|
|
58 |
turn_count = 0
|
59 |
auto_mode = False
|
60 |
|
61 |
+
# === CHAT COMPLETION (Compatible with both APIs) ===
|
62 |
def chat_completion(system, messages, model=CHAT_MODEL):
|
63 |
try:
|
64 |
# Build message list with system prompt
|
65 |
full_messages = [{"role": "system", "content": system}]
|
66 |
full_messages.extend(messages)
|
67 |
|
68 |
+
# Try v1.x API first
|
69 |
+
try:
|
70 |
+
response = openai.chat.completions.create(
|
71 |
+
model=model,
|
72 |
+
messages=full_messages,
|
73 |
+
temperature=0.7,
|
74 |
+
max_tokens=150
|
75 |
+
)
|
76 |
+
return response.choices[0].message.content.strip()
|
77 |
+
except AttributeError:
|
78 |
+
# Fallback to v0.x API
|
79 |
+
response = openai.ChatCompletion.create(
|
80 |
+
model=model,
|
81 |
+
messages=full_messages,
|
82 |
+
temperature=0.7,
|
83 |
+
max_tokens=150
|
84 |
+
)
|
85 |
+
return response['choices'][0]['message']['content'].strip()
|
86 |
+
|
87 |
except Exception as e:
|
88 |
return f"[API Error: {str(e)}]"
|
89 |
|