Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tkinter as tk
|
2 |
+
from tkinter import ttk
|
3 |
+
import requests
|
4 |
+
|
5 |
+
API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
|
6 |
+
headers = {"Authorization": "Bearer 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"}
|
7 |
+
|
8 |
+
API_URL2 = "https://api-inference.huggingface.co/models/valhalla/longformer-base-4096-finetuned-squadv1"
|
9 |
+
headers2 = {"Authorization": "Bearer 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"}
|
10 |
+
|
11 |
+
class App:
|
12 |
+
def __init__(self, root):
|
13 |
+
self.root = root
|
14 |
+
root.title("AI Query UI")
|
15 |
+
|
16 |
+
self.question_label = ttk.Label(root, text="Question:")
|
17 |
+
self.question_entry = ttk.Entry(root, width=50)
|
18 |
+
|
19 |
+
self.context_label = ttk.Label(root, text="Context:")
|
20 |
+
self.context_entry = ttk.Entry(root, width=50)
|
21 |
+
|
22 |
+
self.get_context_button = ttk.Button(root, text="Get Context", command=self.get_context)
|
23 |
+
self.ask_ai_button = ttk.Button(root, text="Ask AI", command=self.ask_ai)
|
24 |
+
|
25 |
+
self.answer_label = ttk.Label(root, text="Answer:")
|
26 |
+
self.answer_entry = ttk.Entry(root, width=50, state="readonly")
|
27 |
+
|
28 |
+
# Grid layout
|
29 |
+
self.question_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
|
30 |
+
self.question_entry.grid(row=0, column=1, padx=10, pady=5, columnspan=2)
|
31 |
+
|
32 |
+
self.context_label.grid(row=1, column=0, padx=10, pady=5, sticky="w")
|
33 |
+
self.context_entry.grid(row=1, column=1, padx=10, pady=5, columnspan=2)
|
34 |
+
|
35 |
+
self.get_context_button.grid(row=2, column=0, padx=10, pady=5)
|
36 |
+
self.ask_ai_button.grid(row=2, column=1, padx=10, pady=5)
|
37 |
+
|
38 |
+
self.answer_label.grid(row=3, column=0, padx=10, pady=5, sticky="w")
|
39 |
+
self.answer_entry.grid(row=3, column=1, padx=10, pady=5, columnspan=2)
|
40 |
+
|
41 |
+
def get_context(self):
|
42 |
+
question = self.question_entry.get()
|
43 |
+
payload = {"question": question}
|
44 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
45 |
+
context = response.json().get("context", "")
|
46 |
+
self.context_entry.delete(0, tk.END)
|
47 |
+
self.context_entry.insert(0, context)
|
48 |
+
|
49 |
+
def ask_ai(self):
|
50 |
+
question = self.question_entry.get()
|
51 |
+
context = self.context_entry.get()
|
52 |
+
payload = {"question": question, "context": context}
|
53 |
+
response = requests.post(API_URL2, headers=headers2, json=payload)
|
54 |
+
answer = response.json().get("answer", "")
|
55 |
+
self.answer_entry.config(state="normal")
|
56 |
+
self.answer_entry.delete(0, tk.END)
|
57 |
+
self.answer_entry.insert(0, answer)
|
58 |
+
self.answer_entry.config(state="readonly")
|
59 |
+
|
60 |
+
if __name__ == "__main__":
|
61 |
+
root = tk.Tk()
|
62 |
+
app = App(root)
|
63 |
+
root.mainloop()
|