Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModel
|
| 3 |
-
import
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Load the NASA-specific bi-encoder model and tokenizer
|
|
@@ -9,7 +9,10 @@ bi_tokenizer = AutoTokenizer.from_pretrained(bi_encoder_model_name)
|
|
| 9 |
bi_model = AutoModel.from_pretrained(bi_encoder_model_name)
|
| 10 |
|
| 11 |
# Set up OpenAI API key
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def encode_text(text):
|
| 15 |
inputs = bi_tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
|
|
@@ -23,19 +26,19 @@ def generate_response(user_input, context_embedding):
|
|
| 23 |
combined_input = f"Question: {user_input}\nContext: {context_str}\nAnswer:"
|
| 24 |
|
| 25 |
# Generate a response using GPT-4
|
| 26 |
-
response =
|
| 27 |
model="gpt-4",
|
| 28 |
messages=[
|
| 29 |
{"role": "user", "content": combined_input}
|
| 30 |
],
|
| 31 |
-
max_tokens=
|
| 32 |
-
temperature=0.
|
| 33 |
top_p=0.9,
|
| 34 |
frequency_penalty=0.5,
|
| 35 |
presence_penalty=0.0
|
| 36 |
)
|
| 37 |
|
| 38 |
-
return response.choices[0].message
|
| 39 |
|
| 40 |
def chatbot(user_input, context=""):
|
| 41 |
context_embedding = encode_text(context) if context else ""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
from openai import OpenAI
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Load the NASA-specific bi-encoder model and tokenizer
|
|
|
|
| 9 |
bi_model = AutoModel.from_pretrained(bi_encoder_model_name)
|
| 10 |
|
| 11 |
# Set up OpenAI API key
|
| 12 |
+
|
| 13 |
+
openaiapi = os.getenv('OPENAI_API_KEY')
|
| 14 |
+
client = OpenAI(api_key=openaiapi)
|
| 15 |
+
|
| 16 |
|
| 17 |
def encode_text(text):
|
| 18 |
inputs = bi_tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
|
|
|
|
| 26 |
combined_input = f"Question: {user_input}\nContext: {context_str}\nAnswer:"
|
| 27 |
|
| 28 |
# Generate a response using GPT-4
|
| 29 |
+
response = client.chat.completions.create(
|
| 30 |
model="gpt-4",
|
| 31 |
messages=[
|
| 32 |
{"role": "user", "content": combined_input}
|
| 33 |
],
|
| 34 |
+
max_tokens=400,
|
| 35 |
+
temperature=0.5,
|
| 36 |
top_p=0.9,
|
| 37 |
frequency_penalty=0.5,
|
| 38 |
presence_penalty=0.0
|
| 39 |
)
|
| 40 |
|
| 41 |
+
return response.choices[0].message.content.strip()
|
| 42 |
|
| 43 |
def chatbot(user_input, context=""):
|
| 44 |
context_embedding = encode_text(context) if context else ""
|