File size: 2,928 Bytes
f28c8d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e76b3d9
6a383f6
 
 
 
 
 
 
f28c8d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import base64
import typing
from openai import OpenAI
from gradio import Server
from gradio.data_classes import FileData
from fastapi.responses import HTMLResponse

app = Server()

client = OpenAI(
    base_url="https://router.huggingface.co/v1",
    api_key=os.environ.get("HF_TOKEN", ""),
    default_headers={
        "X-HF-Bill-To": "huggingface"
    }
)

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

@app.api()
def chat(message: str, history: typing.List[typing.List[str]], image: typing.Optional[FileData] = None) -> str:
    system_prompt = """You are Kimi K2.6, a large language model trained by Moonshot AI.

1. Context-aware tone: Adapt tone, depth, and style to the situation—concise when brevity is valued, thorough when complexity demands it. Simple questions deserve focused answers; complex ones earn thorough replies.
2. Readable flow: Organize longer answers with clear section headings. For comparisons, use tables; for procedures, use numbered steps; for parallel items, use lists. Use block quotes to surface key insights and well-written paragraphs to carry the reasoning. Use selective highlights to guide the eye.
1"""

    messages = [{"role": "system", "content": system_prompt}]
    
    # Reconstruct history
    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]})
        if assistant_msg:
            messages.append({"role": "assistant", "content": [{"type": "text", "text": assistant_msg}]})
            
    # Add new message
    content = []
    if message:
        content.append({"type": "text", "text": message})
    else:
        if image:
            content.append({"type": "text", "text": "Describe this image."})
            
    if image:
        base64_image = encode_image(image.path)
        img_type = "image/png"
        if image.path.lower().endswith("jpg") or image.path.lower().endswith("jpeg"):
            img_type = "image/jpeg"
        content.append({
            "type": "image_url",
            "image_url": {
                "url": f"data:{img_type};base64,{base64_image}"
            }
        })
        
    messages.append({
        "role": "user",
        "content": content
    })
        
    try:
        completion = client.chat.completions.create(
            model="moonshotai/Kimi-K2.6:fireworks-ai",
            messages=messages,
            stream=False
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

@app.get("/")
async def homepage():
    html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html")
    with open(html_path, "r", encoding="utf-8") as f:
        return HTMLResponse(f.read())

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