Spaces:
Sleeping
Sleeping
fixed dual messaggin error
Browse files
app.py
CHANGED
|
@@ -2,19 +2,31 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
|
|
|
| 5 |
# Load environment variables from .env file
|
| 6 |
load_dotenv()
|
| 7 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
|
| 9 |
def chat_with_llm(message, history, temperature=0.7, top_p=1.0, max_tokens=256):
|
| 10 |
"""Function to interact with OpenAI's Chat API with additional controls."""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return response.choices[0].message.content
|
| 19 |
|
| 20 |
# Create Gradio chat interface with sliders for temperature, top_p, and max_tokens
|
|
@@ -27,4 +39,5 @@ demo = gr.ChatInterface(
|
|
| 27 |
]
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
+
|
| 6 |
# Load environment variables from .env file
|
| 7 |
load_dotenv()
|
| 8 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 9 |
|
| 10 |
def chat_with_llm(message, history, temperature=0.7, top_p=1.0, max_tokens=256):
|
| 11 |
"""Function to interact with OpenAI's Chat API with additional controls."""
|
| 12 |
+
|
| 13 |
+
# Convert history (list of tuples) into OpenAI's expected format
|
| 14 |
+
formatted_history = []
|
| 15 |
+
for user_msg, assistant_msg in history:
|
| 16 |
+
formatted_history.append({"role": "user", "content": user_msg})
|
| 17 |
+
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
| 18 |
+
|
| 19 |
+
# Make the API call
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="gpt-3.5-turbo",
|
| 22 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."}] +
|
| 23 |
+
formatted_history +
|
| 24 |
+
[{"role": "user", "content": message}],
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
top_p=top_p,
|
| 27 |
+
max_tokens=max_tokens
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
return response.choices[0].message.content
|
| 31 |
|
| 32 |
# Create Gradio chat interface with sliders for temperature, top_p, and max_tokens
|
|
|
|
| 39 |
]
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Enable sharing for external access
|
| 43 |
+
demo.launch()
|