Nexchan commited on
Commit
0ba8344
·
verified ·
1 Parent(s): e3172a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -1,6 +1,10 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  import json
 
 
 
 
4
 
5
  # Inisialisasi HuggingFace client
6
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
@@ -44,27 +48,35 @@ def process_json(json_input):
44
  chat_history = chat_llama(chat_history)
45
  return json.dumps(chat_history, indent=2), ""
46
 
47
- # Definisikan antarmuka Gradio
48
- def gradio_interface():
49
- with gr.Blocks() as demo:
50
- with gr.Row():
51
- with gr.Column():
52
- chatbot = gr.Chatbot()
53
- msg = gr.Textbox(interactive=True)
54
- with gr.Row():
55
- clear = gr.ClearButton([msg, chatbot])
56
- send_btn = gr.Button("Send", variant='primary')
57
- msg.submit(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
58
- send_btn.click(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
59
-
60
- with gr.Column():
61
- json_input = gr.Textbox(placeholder='Input JSON here...', interactive=True, lines=10)
62
- json_output = gr.Textbox(label='Output JSON', interactive=False, lines=10)
63
- process_btn = gr.Button("Process JSON", variant='primary')
64
- process_btn.click(fn=process_json, inputs=json_input, outputs=[json_output])
 
 
 
 
 
 
65
 
66
- return demo
 
67
 
68
- # Jalankan antarmuka Gradio
69
- demo = gradio_interface()
70
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import JSONResponse
3
  import json
4
+ import univcron
5
+ from huggingface_hub import InferenceClient
6
+
7
+ app = FastAPI()
8
 
9
  # Inisialisasi HuggingFace client
10
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
 
48
  chat_history = chat_llama(chat_history)
49
  return json.dumps(chat_history, indent=2), ""
50
 
51
+ @app.post("/chat_llama")
52
+ async def chat_llama_endpoint(request: Request):
53
+ data = await request.json()
54
+ chat_history = data.get('chat_history', [])
55
+ response = chat_llama(chat_history)
56
+ return JSONResponse(content=response)
57
+
58
+ @app.post("/chat_mem")
59
+ async def chat_mem_endpoint(request: Request):
60
+ data = await request.json()
61
+ message = data.get('message', '')
62
+ chat_history = data.get('chat_history', [])
63
+ response = chat_mem(message, chat_history)
64
+ return JSONResponse(content=response)
65
+
66
+ @app.post("/process_json")
67
+ async def process_json_endpoint(request: Request):
68
+ data = await request.json()
69
+ json_input = data.get('json_input', '')
70
+ response = process_json(json_input)
71
+ return JSONResponse(content=response)
72
+
73
+ def schedule_task():
74
+ print("Scheduled task is running")
75
 
76
+ # Tambahkan tugas terjadwal menggunakan univcron
77
+ univcron.every(10).seconds.do(schedule_task)
78
 
79
+ if __name__ == "__main__":
80
+ import uvicorn
81
+ # Jalankan server FastAPI
82
+ uvicorn.run(app, host="0.0.0.0", port=7860)