File size: 2,881 Bytes
a758968
b01280f
c648bf5
a758968
b01280f
a758968
 
 
 
 
 
 
 
2ec2443
 
a758968
 
 
 
 
 
 
 
2ec2443
a758968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ec2443
 
a758968
 
 
 
 
 
b01280f
c648bf5
a758968
2ec2443
a758968
 
 
2ec2443
 
 
 
a758968
 
 
 
 
 
 
 
 
 
2ec2443
a758968
 
2ec2443
a758968
 
 
 
2ec2443
 
 
 
a758968
 
 
 
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
import os
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

class Chatbot:
    def __init__(self):
        self.api_key = os.getenv("OPENROUTER_API_KEY")
        
        if not self.api_key:
            raise ValueError(
                "OpenRouter API key tidak ditemukan. " +
                "Harap set di Hugging Face Secrets atau file .env"
            )
        
        self.client = OpenAI(
            base_url="https://openrouter.ai/api/v1",
            api_key=self.api_key,
        )
        
        self.model = "deepseek/deepseek-r1-zero:free"
        self.system_prompt = "Anda adalah asisten AI yang membantu dan ramah."

    def generate_response(self, message, history):
        try:
            messages = [{"role": "system", "content": self.system_prompt}]
            
            for i, h in enumerate(history):
                messages.append({
                    "role": "user" if i % 2 == 0 else "assistant",
                    "content": h
                })
            
            messages.append({"role": "user", "content": message})
            
            completion = self.client.chat.completions.create(
                extra_headers={
                    "HTTP-Referer": "https://huggingface.co/spaces",
                    "X-Title": "DeepSeek Chatbot",
                },
                model=self.model,
                messages=messages,
                temperature=0.7,
                max_tokens=500
            )
            
            return completion.choices[0].message.content
            
        except Exception as e:
            print(f"Error: {str(e)}")
            return f"Maaf, terjadi kesalahan: {str(e)}"

try:
    chatbot = Chatbot()
except ValueError as e:
    print(str(e))
    chatbot = None

def respond(message, history):
    if not chatbot:
        return "Error: Inisialisasi chatbot gagal. Periksa konfigurasi API key."
    return chatbot.generate_response(message, history)

examples = [
    "Apa itu machine learning?",
    "Buatkan cerpen tentang robot",
    "Jelaskan hukum termodinamika pertama",
    "Apa perbedaan AI dan manusia?"
]

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🤖 DeepSeek R1 Zero Chatbot
    Chatbot cerdas menggunakan model DeepSeek R1 Zero melalui OpenRouter API
    """)
    
    with gr.Row():
        with gr.Column():
            # Perbaikan: Menghapus parameter yang tidak didukung
            gr.ChatInterface(
                respond,
                examples=examples
            )
        with gr.Column():
            gr.Markdown("""
            ### Tentang Chatbot Ini
            - **Model**: DeepSeek R1 Zero
            - **Platform**: OpenRouter API
            - **Framework**: Gradio
            - **Host**: Hugging Face Spaces
            """)

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