Spaces:
Sleeping
Sleeping
File size: 1,461 Bytes
64e129b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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() |