prithivMLmods commited on
Commit
45721f6
Β·
verified Β·
1 Parent(s): 2295ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -6,6 +6,8 @@ import gradio as gr
6
  import spaces
7
  import torch
8
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
 
 
9
 
10
  DESCRIPTION = """
11
  # GWQ PREV
@@ -27,6 +29,19 @@ model = AutoModelForCausalLM.from_pretrained(
27
  model.config.sliding_window = 4096
28
  model.eval()
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  @spaces.GPU(duration=120)
32
  def generate(
@@ -37,6 +52,7 @@ def generate(
37
  top_p: float = 0.9,
38
  top_k: int = 50,
39
  repetition_penalty: float = 1.2,
 
40
  ) -> Iterator[str]:
41
  conversation = chat_history.copy()
42
  conversation.append({"role": "user", "content": message})
@@ -67,6 +83,10 @@ def generate(
67
  outputs.append(text)
68
  yield "".join(outputs)
69
 
 
 
 
 
70
 
71
  demo = gr.ChatInterface(
72
  fn=generate,
@@ -106,13 +126,13 @@ demo = gr.ChatInterface(
106
  step=0.05,
107
  value=1.2,
108
  ),
 
109
  ],
110
  stop_btn=None,
111
  examples=[
112
  ["Write a Python function to reverses a string if it's length is a multiple of 4. def reverse_string(str1): if len(str1) % 4 == 0: return ''.join(reversed(str1)) return str1 print(reverse_string('abcd')) print(reverse_string('python')) "],
113
  ["Rectangle $ABCD$ is the base of pyramid $PABCD$. If $AB = 10$, $BC = 5$, $\overline{PA}\perp \text{plane } ABCD$, and $PA = 8$, then what is the volume of $PABCD$?"],
114
  ["Difference between List comprehension and Lambda in Python lst = [x ** 2 for x in range (1, 11) if x % 2 == 1] print(lst)"],
115
- ["How many hours does it take a man to eat a Helicopter?"],
116
  ["How Many R's in the Word 'STRAWBERRY' ?"],
117
  ],
118
  cache_examples=False,
@@ -122,6 +142,5 @@ demo = gr.ChatInterface(
122
  fill_height=True,
123
  )
124
 
125
-
126
  if __name__ == "__main__":
127
  demo.queue(max_size=20).launch()
 
6
  import spaces
7
  import torch
8
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
+ from pyvis.network import Network
10
+ import networkx as nx
11
 
12
  DESCRIPTION = """
13
  # GWQ PREV
 
29
  model.config.sliding_window = 4096
30
  model.eval()
31
 
32
+ def create_knowledge_graph(text):
33
+ # Simple example: Create a graph from the text
34
+ G = nx.Graph()
35
+ words = text.split()
36
+ for i in range(len(words) - 1):
37
+ G.add_edge(words[i], words[i + 1])
38
+ return G
39
+
40
+ def visualize_knowledge_graph(graph):
41
+ net = Network(notebook=True, cdn_resources='in_line')
42
+ net.from_nx(graph)
43
+ net.show("knowledge_graph.html")
44
+ return "knowledge_graph.html"
45
 
46
  @spaces.GPU(duration=120)
47
  def generate(
 
52
  top_p: float = 0.9,
53
  top_k: int = 50,
54
  repetition_penalty: float = 1.2,
55
+ visualize_graph: bool = False,
56
  ) -> Iterator[str]:
57
  conversation = chat_history.copy()
58
  conversation.append({"role": "user", "content": message})
 
83
  outputs.append(text)
84
  yield "".join(outputs)
85
 
86
+ if visualize_graph:
87
+ graph = create_knowledge_graph("".join(outputs))
88
+ graph_file = visualize_knowledge_graph(graph)
89
+ yield f"Knowledge graph saved to {graph_file}"
90
 
91
  demo = gr.ChatInterface(
92
  fn=generate,
 
126
  step=0.05,
127
  value=1.2,
128
  ),
129
+ gr.Checkbox(label="Visualize Knowledge Graph", value=False),
130
  ],
131
  stop_btn=None,
132
  examples=[
133
  ["Write a Python function to reverses a string if it's length is a multiple of 4. def reverse_string(str1): if len(str1) % 4 == 0: return ''.join(reversed(str1)) return str1 print(reverse_string('abcd')) print(reverse_string('python')) "],
134
  ["Rectangle $ABCD$ is the base of pyramid $PABCD$. If $AB = 10$, $BC = 5$, $\overline{PA}\perp \text{plane } ABCD$, and $PA = 8$, then what is the volume of $PABCD$?"],
135
  ["Difference between List comprehension and Lambda in Python lst = [x ** 2 for x in range (1, 11) if x % 2 == 1] print(lst)"],
 
136
  ["How Many R's in the Word 'STRAWBERRY' ?"],
137
  ],
138
  cache_examples=False,
 
142
  fill_height=True,
143
  )
144
 
 
145
  if __name__ == "__main__":
146
  demo.queue(max_size=20).launch()