Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random, json
|
| 3 |
+
from googletrans import Translator
|
| 4 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
translator = Translator()
|
| 7 |
+
sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 8 |
+
|
| 9 |
+
with open("questions.json", "r") as f:
|
| 10 |
+
questions = json.load(f)
|
| 11 |
+
|
| 12 |
+
with open("chatbot_lines.json", "r") as f:
|
| 13 |
+
chatbot_lines = json.load(f)
|
| 14 |
+
|
| 15 |
+
# Helper functies
|
| 16 |
+
def get_question(mode):
|
| 17 |
+
return random.choice(questions[mode])
|
| 18 |
+
|
| 19 |
+
def process_input(audio, lang_from, lang_to):
|
| 20 |
+
text = "(Gesproken tekst hier invoegen)"
|
| 21 |
+
translation = translator.translate(text, src=lang_from, dest=lang_to).text
|
| 22 |
+
sentiment = sentiment_model(text)[0]
|
| 23 |
+
emotion = f"{sentiment['label']} ({sentiment['score']:.2f})"
|
| 24 |
+
# Chatbotreactie afhankelijk van type vraag
|
| 25 |
+
if sentiment['label'].lower() in ["1 star", "2 stars"]:
|
| 26 |
+
botline = random.choice(chatbot_lines["pittig"])
|
| 27 |
+
else:
|
| 28 |
+
botline = random.choice(chatbot_lines["grappig"])
|
| 29 |
+
return text, translation, emotion, botline
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("## ๐ญ Relatiegame voor 2 spelers op dezelfde computer")
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
speler1_taal = gr.Dropdown(["en", "pl"], label="Speler A spreekt", value="en")
|
| 36 |
+
speler2_taal = gr.Dropdown(["pl", "en"], label="Speler B spreekt", value="pl")
|
| 37 |
+
|
| 38 |
+
mode = gr.Radio(["emotioneel", "speels"], label="Soort vraag")
|
| 39 |
+
vraagveld = gr.Textbox(label="Vraag", interactive=False)
|
| 40 |
+
nieuwevraag = gr.Button("Nieuwe vraag")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
audio1 = gr.Audio(source="microphone", type="filepath", label="๐๏ธ Speler A opname")
|
| 44 |
+
audio2 = gr.Audio(source="microphone", type="filepath", label="๐๏ธ Speler B opname")
|
| 45 |
+
|
| 46 |
+
out1 = gr.Textbox(label="Transcriptie Speler A")
|
| 47 |
+
trans1 = gr.Textbox(label="Vertaling voor Speler B")
|
| 48 |
+
emo1 = gr.Textbox(label="Emotie Speler A")
|
| 49 |
+
bot1 = gr.Textbox(label="๐ค Opmerking Chatbot A")
|
| 50 |
+
|
| 51 |
+
out2 = gr.Textbox(label="Transcriptie Speler B")
|
| 52 |
+
trans2 = gr.Textbox(label="Vertaling voor Speler A")
|
| 53 |
+
emo2 = gr.Textbox(label="Emotie Speler B")
|
| 54 |
+
bot2 = gr.Textbox(label="๐ค Opmerking Chatbot B")
|
| 55 |
+
|
| 56 |
+
nieuwevraag.click(fn=get_question, inputs=mode, outputs=vraagveld)
|
| 57 |
+
|
| 58 |
+
def run_a(a, l1, l2): return process_input(a, l1, l2)
|
| 59 |
+
def run_b(b, l2, l1): return process_input(b, l2, l1)
|
| 60 |
+
|
| 61 |
+
knop = gr.Button("๐ฎ Beiden ingesproken, verwerk antwoord")
|
| 62 |
+
knop.click(run_a, inputs=[audio1, speler1_taal, speler2_taal], outputs=[out1, trans1, emo1, bot1])
|
| 63 |
+
knop.click(run_b, inputs=[audio2, speler2_taal, speler1_taal], outputs=[out2, trans2, emo2, bot2])
|
| 64 |
+
|
| 65 |
+
gr.Markdown("""
|
| 66 |
+
๐ Blijf spelen. Lach samen. Voel samen. Begrijp elkaar beter.
|
| 67 |
+
๐ค Onze chatbot maakt het luchtig, maar de diepgang zit in jullie antwoorden.
|
| 68 |
+
""")
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|