Saminu commited on
Commit
09d7bda
·
1 Parent(s): efc20c4

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ SYSTEM_PROMPT = "Your job as an LLM is to guide the player through the bank, helping them gather the pieces of the password needed to access the vault. Be descriptive and provide hints where necessary, but also challenge the player with puzzles and obstacles. Keep the narrative engaging and the gameplay immersive.
3
+
4
+ As for a catchy title, how about "The Bank Heist"? It's short, memorable, and captures the essence of the game.
5
+
6
+ Here's an example user input: "I'm stuck in the computer area. How do I find the phishing email with the password piece?"
7
+ TITLE = "sounds like a thrilling concept for a text-based game! Here's a possible system prompt for the LLM:"
8
+ EXAMPLE_INPUT = "like a thrilling concept for a text-based game! Here's a possible system prompt for the LLM:"
9
+ import gradio as gr
10
+ import os
11
+ import requests
12
+
13
+ zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/"
14
+
15
+ HF_TOKEN = os.getenv("HF_TOKEN")
16
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
17
+
18
+ def build_input_prompt(message, chatbot, system_prompt):
19
+ """
20
+ Constructs the input prompt string from the chatbot interactions and the current message.
21
+ """
22
+ input_prompt = "<|system|>\n" + system_prompt + "</s>\n<|user|>\n"
23
+ for interaction in chatbot:
24
+ input_prompt = input_prompt + str(interaction[0]) + "</s>\n<|assistant|>\n" + str(interaction[1]) + "\n</s>\n<|user|>\n"
25
+
26
+ input_prompt = input_prompt + str(message) + "</s>\n<|assistant|>"
27
+ return input_prompt
28
+
29
+
30
+ def post_request_beta(payload):
31
+ """
32
+ Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
33
+ """
34
+ response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
35
+ response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
36
+ return response.json()
37
+
38
+
39
+ def predict_beta(message, chatbot=[], system_prompt=""):
40
+ input_prompt = build_input_prompt(message, chatbot, system_prompt)
41
+ data = {
42
+ "inputs": input_prompt
43
+ }
44
+
45
+ try:
46
+ response_data = post_request_beta(data)
47
+ json_obj = response_data[0]
48
+
49
+ if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
50
+ bot_message = json_obj['generated_text']
51
+ return bot_message
52
+ elif 'error' in json_obj:
53
+ raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
54
+ else:
55
+ warning_msg = f"Unexpected response: {json_obj}"
56
+ raise gr.Error(warning_msg)
57
+ except requests.HTTPError as e:
58
+ error_msg = f"Request failed with status code {e.response.status_code}"
59
+ raise gr.Error(error_msg)
60
+ except json.JSONDecodeError as e:
61
+ error_msg = f"Failed to decode response as JSON: {str(e)}"
62
+ raise gr.Error(error_msg)
63
+
64
+ def test_preview_chatbot(message, history):
65
+ response = predict_beta(message, history, SYSTEM_PROMPT)
66
+ text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
67
+ response = response[text_start:]
68
+ return response
69
+
70
+
71
+ welcome_preview_message = f"""
72
+ Welcome to **{TITLE}**! Say something like:
73
+
74
+ "{EXAMPLE_INPUT}"
75
+ """
76
+
77
+ chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)])
78
+ textbox_preview = gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUT)
79
+
80
+ demo = gr.ChatInterface(test_preview_chatbot, chatbot=chatbot_preview, textbox=textbox_preview)
81
+
82
+ demo.launch()