Asaad Almutareb commited on
Commit
8b92625
·
1 Parent(s): ef85c0b

added app.py wrapper for hf spaces demo

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from hf_mixtral_agent import agent_executor
3
+ from innovation_pathfinder_ai.source_container.container import (
4
+ all_sources
5
+ )
6
+
7
+ if __name__ == "__main__":
8
+
9
+ def add_text(history, text):
10
+ history = history + [(text, None)]
11
+ return history, ""
12
+
13
+ def bot(history):
14
+ response = infer(history[-1][0], history)
15
+ sources = collect_urls(all_sources)
16
+ src_list = '\n'.join(sources)
17
+ response_w_sources = response['output']+"\n\n\n Sources: \n\n\n"+src_list
18
+ history[-1][1] = response_w_sources
19
+ return history
20
+
21
+ def infer(question, history):
22
+ query = question
23
+ result = agent_executor.invoke(
24
+ {
25
+ "input": question,
26
+ }
27
+ )
28
+ return result
29
+
30
+ def vote(data: gr.LikeData):
31
+ if data.liked:
32
+ print("You upvoted this response: " + data.value)
33
+ else:
34
+ print("You downvoted this response: " + data.value)
35
+
36
+ def collect_urls(data_list):
37
+ urls = []
38
+ for item in data_list:
39
+ # Check if item is a string and contains 'link:'
40
+ if isinstance(item, str) and 'link:' in item:
41
+ start = item.find('link:') + len('link: ')
42
+ end = item.find(',', start)
43
+ url = item[start:end if end != -1 else None].strip()
44
+ urls.append(url)
45
+ # Check if item is a dictionary and has 'Entry ID'
46
+ elif isinstance(item, dict) and 'Entry ID' in item:
47
+ urls.append(item['Entry ID'])
48
+ return urls
49
+
50
+ css="""
51
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
52
+ """
53
+
54
+ title = """
55
+ <div style="text-align: center;max-width: 700px;">
56
+ <p>Hello Dave, how can I help today?<br />
57
+ </div>
58
+ """
59
+
60
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
+ with gr.Tab("Google|Wikipedia|Arxiv"):
62
+ with gr.Column(elem_id="col-container"):
63
+ gr.HTML(title)
64
+ with gr.Row():
65
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
66
+ chatbot = gr.Chatbot([], elem_id="chatbot")
67
+ chatbot.like(vote, None, None)
68
+ clear = gr.Button("Clear")
69
+ question.submit(add_text, [chatbot, question], [chatbot, question], queue=False).then(
70
+ bot, chatbot, chatbot
71
+ )
72
+ clear.click(lambda: None, None, chatbot, queue=False)
73
+
74
+ demo.queue()
75
+ demo.launch(debug=True)
76
+
77
+
78
+ x = 0 # for debugging purposes