Spaces:
Build error
Build error
File size: 2,837 Bytes
af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 af0e6f0 b04f907 |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import gradio as gr
import openai
initial_prompt = "You are a helpful assistant."
def parse_text(text):
lines = text.split("\n")
for i,line in enumerate(lines):
if "```" in line:
items = line.split('`')
if items[-1]:
lines[i] = f'<pre><code class="{items[-1]}">'
else:
lines[i] = f'</code></pre>'
else:
if i>0:
line = line.replace("<", "<")
line = line.replace(">", ">")
lines[i] = '<br/>'+line.replace(" ", " ")
return "".join(lines)
def get_response(system, context, raw = False):
openai.api_key = "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"
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"{system}\n\n{context}",
temperature=0.7,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None,
)
if raw:
return response
else:
statistics = f'This conversation Tokens usage【{response["total_characters"]} / 2048】 ( Question + above {response["prompt"]["length"]},Answer {response["choices"][0]["length"]} )'
message = response.choices[0].text
message_with_stats = f'{message}\n\n{statistics}'
return message, parse_text(message_with_stats)
def predict(chatbot, input_sentence, system, context):
if len(input_sentence) == 0:
return []
context.append(input_sentence)
message, message_with_stats = get_response(system, context)
context.append(message)
chatbot.append((input_sentence, message_with_stats))
return chatbot, context
def retry(chatbot, system, context):
if len(context) == 0:
return [], []
message, message_with_stats = get_response(system, context[:-1])
context[-1] = message
chatbot[-1] = (context[-2], message_with_stats)
return chatbot, context
def delete_last_conversation(chatbot, context):
if len(context) == 0:
return [], []
chatbot = chatbot[:-1]
context = context[:-2]
return chatbot, context
def reset_state():
return [], []
def update_system(new_system_prompt):
return new_system_prompt
title = """<h1 align="center">Tu întrebi și eu răspund.</h1>"""
description = """<div align=center>
Will not describe your needs to ChatGPT?You Use [ChatGPT Shortcut](https://newzone.top/chatgpt/)
</div>
"""
gr.Interface(
predict,
inputs=["textbox", "text", "text", "text"],
outputs="text",
title=title,
description=description,
allow_flagging=False,
theme="huggingface",
layout="vertical",
examples=[["What's your name?", "I don't have a name. How can I assist you today?", "", ""]],
interpretation="default"
).launch()
|