Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import json | |
import requests | |
backend_url = os.getenv("BACKEND_URL") | |
print(backend_url) | |
def format_messages(history:str): | |
converted_list = [] | |
for item in history: | |
user_dict = { | |
"role": "user", | |
"parts": [item[0]] | |
} | |
model_dict = { | |
"role": "model", | |
"parts": [item[1]] | |
} | |
converted_list.append(user_dict) | |
converted_list.append(model_dict) | |
to_pass = { | |
"history" : converted_list | |
} | |
return to_pass | |
def get_completion(query, formatted_history): | |
print(formatted_history) | |
response = requests.post(f"{backend_url}/?q={query}", json=formatted_history) | |
print(response.json()) | |
if response.status_code == 200: | |
return response.json()["completion"] | |
else: | |
return None | |
def yes_man(message, history): | |
formatted_messages = format_messages(history=history) | |
response = get_completion(query=message, formatted_history=formatted_messages) | |
return response | |
gr.ChatInterface( | |
yes_man, | |
chatbot=gr.Chatbot(height=500), | |
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7), | |
title="Yes Man", | |
description="Ask Yes Man any question", | |
theme="soft", | |
examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"], | |
cache_examples=True, | |
retry_btn=None, | |
undo_btn="Delete Previous", | |
clear_btn="Clear", | |
).launch() |