Spaces:
Running
Running
File size: 2,917 Bytes
d435c86 f5b9b37 e47edb7 f5b9b37 d435c86 f7461e8 d435c86 f7461e8 d435c86 a51f836 d435c86 d48e96c c78d430 a2ff132 c78d430 d48e96c c78d430 a2ff132 d435c86 807df8d 5839dd1 e428b8d 5839dd1 e428b8d 5839dd1 e428b8d 5839dd1 e428b8d 5839dd1 807df8d 5d2cd5f 53ac0fd 5d2cd5f 807df8d d435c86 c78d430 d435c86 807df8d c78d430 d435c86 a2ff132 807df8d c78d430 5d2cd5f 807df8d efd2301 c78d430 |
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 100 |
import requests
import gradio as gr
import json
def get_text_response(prompt):
api_url = "http://35.233.231.20:5003/api/generate"
data_payload = {
"model": "llama3",
"prompt": prompt,
"stream": True
}
full_response = ""
with requests.post(api_url, json=data_payload, stream=True) as response:
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
json_line = decoded_line.strip(',')
try:
response_json = json.loads(json_line)
text_response = response_json.get("response", "")
done = response_json.get("done", False)
full_response += text_response
yield full_response
if done:
break
except json.JSONDecodeError as e:
continue
def clear_chat():
return "", ""
css = """
<style>
.header {
font-size: 24px;
font-weight: bold;
text-align: center;
margin-top: 1rem;
margin-bottom: 2rem;
}
button.gr-button {
background-color: #7D3C98;
color: white;
padding: 0.5rem 1rem;
font-size: 1rem;
border-radius: 0.5rem;
}
button.gr-button:hover {
background-color: #6C3483;
}
.acknowledgments {
margin-top: 20px;
text-align: center;
}
.gr-textbox, button.gr-button, .gr-output {
height: 2.5rem;
}
</style>
"""
with gr.Blocks(css=css) as demo:
gr.HTML(
"""
<div style="text-align: center; max-width: 650px; margin: 0 auto; padding-top: 7px;">
<div
style="
display: inline-flex;
align-items: center;
gap: 0.8rem;
font-size: 1.75rem;
"
>
<h1 style="font-weight: 900; margin-bottom: 7px;">
Fair Compute Llama-3 Chat
</h1>
</div>
</div>
"""
)
with gr.Row():
prompt = gr.Textbox(label="Enter your prompt", lines=5, placeholder="Type something...")
submit_button = gr.Button("Submit")
clear_button = gr.Button("Clear")
output = gr.Textbox(label="Response", lines=10, placeholder="Response will appear here...")
submit_button.click(fn=get_text_response, inputs=prompt, outputs=output)
clear_button.click(fn=clear_chat, inputs=[], outputs=[prompt, output])
gr.HTML(
"""
<div class="acknowledgments">
<p>Run AI models on your home computers, powered by <a href="https://faircompute.com/" style="text-decoration: underline;" target="_blank">FairCompute</a></p>
</div>
"""
)
if __name__ == "__main__":
demo.launch()
|