Lenylvt commited on
Commit
d4af723
·
verified ·
1 Parent(s): 7cb0b8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -13,17 +13,15 @@ def format_prompt(message, history):
13
  prompt += f"[INST] {message} [/INST]"
14
  return prompt
15
 
16
- def generate_from_file(file_path, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
17
- # Read the file content
18
- with open(file_path, 'r', encoding='utf-8') as file:
19
  file_content = file.read()
20
 
21
- # You might need to modify this part to fit how you want to use the file content in your prompt
22
- prompt = file_content[:1000] # Example: using first 1000 characters of the file content
 
23
 
24
- return generate(prompt, history, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
25
-
26
- def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
27
  temperature = float(temperature)
28
  if temperature < 1e-2:
29
  temperature = 1e-2
@@ -38,18 +36,25 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256
38
  seed=42,
39
  )
40
 
41
- formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
42
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
43
  output = ""
44
 
45
  for response in stream:
46
  output += response.token.text
47
- yield output
48
  return output
49
 
 
 
 
 
 
 
 
 
50
  iface = gr.Interface(
51
- fn=generate_from_file,
52
- inputs=[gr.File(label="Upload File"), gr.State(), gr.Textbox(label="System Prompt")],
53
  outputs="text",
54
  title="SRT File Translation",
55
  concurrency_limit=20,
 
13
  prompt += f"[INST] {message} [/INST]"
14
  return prompt
15
 
16
+ def generate_from_srt(file_path, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
17
+ # Assuming the file content is directly usable as a prompt
18
+ with open(file_path, "r", encoding="utf-8") as file:
19
  file_content = file.read()
20
 
21
+ # Process the SRT file content as needed before using it as a prompt
22
+ # For example, extracting text and removing timestamps if necessary
23
+ # Here, directly using the file content for simplicity
24
 
 
 
 
25
  temperature = float(temperature)
26
  if temperature < 1e-2:
27
  temperature = 1e-2
 
36
  seed=42,
37
  )
38
 
39
+ formatted_prompt = format_prompt(file_content, [])
40
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
41
  output = ""
42
 
43
  for response in stream:
44
  output += response.token.text
 
45
  return output
46
 
47
+ def handle_file(file, **kwargs):
48
+ # Save the uploaded file temporarily to read it
49
+ file_path = file.name
50
+ with open(file_path, "wb") as f:
51
+ f.write(file.read())
52
+
53
+ return generate_from_srt(file_path, **kwargs)
54
+
55
  iface = gr.Interface(
56
+ fn=handle_file,
57
+ inputs=gr.File(label="Upload SRT File"),
58
  outputs="text",
59
  title="SRT File Translation",
60
  concurrency_limit=20,