Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,88 @@
|
|
| 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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests, json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
public_ip = '71.202.66.108'
|
| 5 |
+
|
| 6 |
+
model = 'llama3.1:latest' #You can replace the model name if needed
|
| 7 |
+
context = []
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
# ollama_serve = f"http://{mac_pro_ip}:11434/api/generate"
|
| 13 |
+
ollama_serve = f"http://{public_ip}:11434/api/generate"
|
| 14 |
+
|
| 15 |
+
#Call Ollama API
|
| 16 |
+
def generate(prompt, context, top_k, top_p, temp):
|
| 17 |
+
r = requests.post(ollama_serve,
|
| 18 |
+
json={
|
| 19 |
+
'model': model,
|
| 20 |
+
'prompt': prompt,
|
| 21 |
+
'context': context,
|
| 22 |
+
'options':{
|
| 23 |
+
'top_k': top_k,
|
| 24 |
+
'temperature':top_p,
|
| 25 |
+
'top_p': temp
|
| 26 |
+
}
|
| 27 |
+
},
|
| 28 |
+
stream=True)
|
| 29 |
+
r.raise_for_status()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
response = ""
|
| 33 |
+
|
| 34 |
+
for line in r.iter_lines():
|
| 35 |
+
body = json.loads(line)
|
| 36 |
+
response_part = body.get('response', '')
|
| 37 |
+
print(response_part)
|
| 38 |
+
if 'error' in body:
|
| 39 |
+
raise Exception(body['error'])
|
| 40 |
+
|
| 41 |
+
response += response_part
|
| 42 |
+
|
| 43 |
+
if body.get('done', False):
|
| 44 |
+
context = body.get('context', [])
|
| 45 |
+
return response, context
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def chat(input, chat_history, top_k, top_p, temp):
|
| 50 |
+
|
| 51 |
+
chat_history = chat_history or []
|
| 52 |
+
|
| 53 |
+
global context
|
| 54 |
+
output, context = generate(input, context, top_k, top_p, temp)
|
| 55 |
+
|
| 56 |
+
chat_history.append((input, output))
|
| 57 |
+
|
| 58 |
+
return chat_history, chat_history
|
| 59 |
+
#the first history in return history, history is meant to update the
|
| 60 |
+
#chatbot widget, and the second history is meant to update the state
|
| 61 |
+
#(which is used to maintain conversation history across interactions)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
#########################Gradio Code##########################
|
| 65 |
+
block = gr.Blocks()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with block:
|
| 69 |
+
|
| 70 |
+
gr.Markdown("""<h1><center> Trashcan AI </center></h1>
|
| 71 |
+
""")
|
| 72 |
+
|
| 73 |
+
chatbot = gr.Chatbot()
|
| 74 |
+
message = gr.Textbox(placeholder="Type here")
|
| 75 |
+
|
| 76 |
+
state = gr.State()
|
| 77 |
+
with gr.Row():
|
| 78 |
+
top_k = gr.Slider(0.0,100.0, label="top_k", value=40, info="Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)")
|
| 79 |
+
top_p = gr.Slider(0.0,1.0, label="top_p", value=0.9, info=" Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)")
|
| 80 |
+
temp = gr.Slider(0.0,2.0, label="temperature", value=0.8, info="The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
submit = gr.Button("SEND")
|
| 84 |
+
|
| 85 |
+
submit.click(chat, inputs=[message, state, top_k, top_p, temp], outputs=[chatbot, state])
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
block.launch()
|