File size: 937 Bytes
56261ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
import time

# Use a smaller model if HF space has <1GB RAM
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")

def chat_with_ai(prompt):
    timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if not prompt:
        return f"[{timestamp}] Error: No prompt received."
    
    try:
        response = generator(prompt, max_new_tokens=50)[0]['generated_text']
        return f"[{timestamp}] {response}"
    except Exception as e:
        return f"[{timestamp}] Internal Error: {str(e)}"

iface = gr.Interface(
    fn=chat_with_ai,
    inputs=gr.Textbox(label="Prompt"),
    outputs=gr.Textbox(label="Response"),
    title="ESP8266 Web AI",
    live=True,
    description="AI API endpoint for microcontrollers. Send POST to /api/predict."
)

app = gr.mount_gradio_app(app=iface.launch(share=False, server_name="0.0.0.0", server_port=7860), path="/")