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 = """ """ with gr.Blocks(css=css) as demo: gr.HTML( """

Fair Compute Llama-3 Chat

""" ) 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( """

Run AI models on your home computers, powered by FairCompute

""" ) if __name__ == "__main__": demo.launch()