Thiloid commited on
Commit
4d9ef07
·
verified ·
1 Parent(s): 31674f4

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +23 -49
run.py CHANGED
@@ -96,8 +96,22 @@ def format_prompt(message, history):
96
  return prompt
97
 
98
  def response(prompt, history, temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0):
99
- global global_url
100
- print(f"Working with URL: {global_url}") # You can use the URL here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  temperature = float(temperature)
102
  if temperature < 1e-2: temperature = 1e-2
103
  top_p = float(top_p)
@@ -130,51 +144,11 @@ def response(prompt, history, temperature=0.9, max_new_tokens=500, top_p=0.95, r
130
  now = str(datetime.now())
131
  save_to_sheet(now, prompt, output)
132
  yield output
 
 
 
 
 
 
133
 
134
- def js_code():
135
- return """
136
- <script>
137
- function getUrl() {
138
- const url = window.location.href;
139
- const xhr = new XMLHttpRequest();
140
- xhr.open("POST", "/submit_url", true);
141
- xhr.setRequestHeader("Content-Type", "application/json");
142
- xhr.onreadystatechange = function() {
143
- if (xhr.readyState === 4 && xhr.status === 200) {
144
- console.log("URL submitted successfully");
145
- }
146
- };
147
- xhr.send(JSON.stringify({ url: url }));
148
- }
149
- window.onload = getUrl;
150
- </script>
151
- """
152
-
153
- def submit_url(request):
154
- global global_url
155
- data = json.loads(request.body.decode("utf-8"))
156
- url = data.get("url")
157
- global_url = url # Save the URL in the global variable
158
- print(f"Received URL: {url}")
159
- return {"status": "success"}
160
-
161
- # Define the Gradio interface
162
- def create_interface():
163
- # Create a basic gr.Interface to include the JavaScript and handle URL submissions
164
- with gr.Blocks() as app:
165
- gr.HTML(js_code) # Include the JavaScript in the app
166
- gr.ChatInterface(
167
- fn=response,
168
- chatbot=gr.Chatbot(
169
- value=[[None, "Herzlich willkommen! Ich bin Chätti ein KI-basiertes Studienassistenzsystem, das für jede Anfrage die am besten Studieninformationen empfiehlt.<br>Erzähle mir, was du gerne tust!"]],
170
- render_markdown=True
171
- ),
172
- title="German Studyhelper Chätti"
173
- ).launch(
174
- share=True
175
- )
176
-
177
- # Launch the Gradio app with URL capturing
178
- if __name__ == "__main__":
179
- create_interface()
180
- print("Interface up and running!")
 
96
  return prompt
97
 
98
  def response(prompt, history, temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0):
99
+ global_url = "" # Initialize URL variable
100
+ # JavaScript code to extract URL from the browser
101
+ js_code = """
102
+ <script>
103
+ function extractUrl() {
104
+ return window.location.href;
105
+ }
106
+ </script>
107
+ """
108
+
109
+ # Extract URL using JavaScript
110
+ url_script = '<script>var url = extractUrl(); document.getElementById("url").innerText = url;</script>'
111
+ url_extracted = "<div id='url'></div>" # Placeholder for URL extraction
112
+
113
+ print(f"Working with URL: {url_extracted}")
114
+
115
  temperature = float(temperature)
116
  if temperature < 1e-2: temperature = 1e-2
117
  top_p = float(top_p)
 
144
  now = str(datetime.now())
145
  save_to_sheet(now, prompt, output)
146
  yield output
147
+
148
+ gr.ChatInterface(
149
+ response,
150
+ chatbot=gr.Chatbot(value=[[None, "Herzlich willkommen! Ich bin Chätti ein KI-basiertes Studienassistenzsystem, das für jede Anfrage die am besten Studieninformationen empfiehlt.<br>Erzähle mir, was du gerne tust!"]], render_markdown=True),
151
+ title="German Studyhelper Chätti"
152
+ ).queue().launch(share=True)
153
 
154
+ print("Interface up and running!")