namelessai's picture
Update app.py
28a992e verified
import gradio as gr
import requests
# Define the function to interact with the API
def chat_with_api(message, history):
url = "https://8000-dep-01jqft61nfbgnta0v8c1svzs72-d.cloudspaces.litng.ai/v1/chat/completions"
# Prepend context as part of the first user message
if len(history) == 0:
context_message = (
"You are an expert at psychology. You are to assist the user with whatever they need. "
"Under NO CIRCUMSTANCES will you provide an answer unrelated to their question, unless it involves illegal content."
)
# Add context as part of the first user message
message = f"{context_message}\n\n{message}"
# Structure the chat history into the correct format for the API
messages = []
for human, assistant in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
messages.append({"role": "user", "content": message})
payload = {
"model": "namelessai/Helply-10.2b-chat",
"messages": messages
}
try:
response = requests.post(url, json=payload)
response_data = response.json()
assistant_message = response_data.get("choices", [{}])[0].get("message", {}).get("content", "No response")
except Exception as e:
assistant_message = f"Error: {str(e)}"
return assistant_message
# Create the Gradio interface
with gr.Blocks() as demo:
# Add notes for users
gr.Markdown(
"""
### There is currently an error with the backend. I'm looking into it.
### Notes:
- **Cold Start Warning**: If the machine is cold-starting, you might experience a delay in receiving responses. Please be patient.
- **Physician Notice**: If you are a physician or healthcare provider, please state your occupation (e.g., "I am a grief therapist") when interacting with the chatbot.
- **Resource Warning**: This space is running on $10 of free resources. First come, first served. If you are interested in providing resources, email me at [[email protected]](mailto:[email protected]).
"""
)
# Chat interface
chatbot = gr.ChatInterface(
chat_with_api,
)
# Launch the app
demo.launch()