File size: 2,597 Bytes
1349898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tkinter as tk
from tkinter import ttk
import requests

API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
headers = {"Authorization": "Bearer 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"}

API_URL2 = "https://api-inference.huggingface.co/models/valhalla/longformer-base-4096-finetuned-squadv1"
headers2 = {"Authorization": "Bearer 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"}

class App:
    def __init__(self, root):
        self.root = root
        root.title("AI Query UI")

        self.question_label = ttk.Label(root, text="Question:")
        self.question_entry = ttk.Entry(root, width=50)

        self.context_label = ttk.Label(root, text="Context:")
        self.context_entry = ttk.Entry(root, width=50)

        self.get_context_button = ttk.Button(root, text="Get Context", command=self.get_context)
        self.ask_ai_button = ttk.Button(root, text="Ask AI", command=self.ask_ai)

        self.answer_label = ttk.Label(root, text="Answer:")
        self.answer_entry = ttk.Entry(root, width=50, state="readonly")

        # Grid layout
        self.question_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
        self.question_entry.grid(row=0, column=1, padx=10, pady=5, columnspan=2)

        self.context_label.grid(row=1, column=0, padx=10, pady=5, sticky="w")
        self.context_entry.grid(row=1, column=1, padx=10, pady=5, columnspan=2)

        self.get_context_button.grid(row=2, column=0, padx=10, pady=5)
        self.ask_ai_button.grid(row=2, column=1, padx=10, pady=5)

        self.answer_label.grid(row=3, column=0, padx=10, pady=5, sticky="w")
        self.answer_entry.grid(row=3, column=1, padx=10, pady=5, columnspan=2)

    def get_context(self):
        question = self.question_entry.get()
        payload = {"question": question}
        response = requests.post(API_URL, headers=headers, json=payload)
        context = response.json().get("context", "")
        self.context_entry.delete(0, tk.END)
        self.context_entry.insert(0, context)

    def ask_ai(self):
        question = self.question_entry.get()
        context = self.context_entry.get()
        payload = {"question": question, "context": context}
        response = requests.post(API_URL2, headers=headers2, json=payload)
        answer = response.json().get("answer", "")
        self.answer_entry.config(state="normal")
        self.answer_entry.delete(0, tk.END)
        self.answer_entry.insert(0, answer)
        self.answer_entry.config(state="readonly")

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()