Spaces:
Running
on
Zero
Running
on
Zero
Upload 2 files
Browse files- chatbot.py +40 -0
- ocr.py +10 -0
chatbot.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
7 |
+
|
8 |
+
def tanya_ai(prompt_pengguna):
|
9 |
+
headers = {
|
10 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
11 |
+
"Content-Type": "application/json",
|
12 |
+
"Referer": "https://openrouter.ai",
|
13 |
+
"X-Title": "bintang-ai"
|
14 |
+
}
|
15 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
16 |
+
data = {
|
17 |
+
"model": "mistralai/mistral-7b-instruct:free",
|
18 |
+
"messages": [
|
19 |
+
{"role": "user", "content": prompt_pengguna}
|
20 |
+
],
|
21 |
+
"temperature": 0.7
|
22 |
+
}
|
23 |
+
|
24 |
+
try:
|
25 |
+
response = requests.post(url, headers=headers, json=data)
|
26 |
+
response.raise_for_status()
|
27 |
+
result = response.json()
|
28 |
+
return result["choices"][0]["message"]["content"]
|
29 |
+
except Exception as e:
|
30 |
+
return f"Terjadi kesalahan: {e}"
|
31 |
+
|
32 |
+
def jalankan_chatbot():
|
33 |
+
print("🔌 AI Kelistrikan (Online - Bintang Edition) ⚡")
|
34 |
+
print("Tanyakan apa saja tentang listrik. Ketik 'keluar' untuk berhenti.")
|
35 |
+
while True:
|
36 |
+
pertanyaan = input("Kamu: ")
|
37 |
+
if pertanyaan.lower() == "keluar":
|
38 |
+
break
|
39 |
+
jawaban = tanya_ai(pertanyaan)
|
40 |
+
print("AI:", jawaban)
|
ocr.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytesseract
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
def baca_teks_dari_gambar(nama_file_gambar):
|
5 |
+
try:
|
6 |
+
gambar = Image.open(nama_file_gambar)
|
7 |
+
teks = pytesseract.image_to_string(gambar, lang='eng')
|
8 |
+
return teks
|
9 |
+
except Exception as e:
|
10 |
+
return f"Terjadi kesalahan saat membaca gambar: {e}"
|