Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,9 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
import torch
|
4 |
import os
|
5 |
-
|
6 |
-
|
7 |
-
# Authenticate with Hugging Face using the environment variable
|
8 |
-
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
9 |
-
login(token=huggingface_token)
|
10 |
-
|
11 |
-
# Load the tokenizer and model from Hugging Face
|
12 |
-
@st.cache_resource
|
13 |
-
def load_model():
|
14 |
-
model_name = "meta-llama/Meta-Llama-3.1-70B-Instruct"
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
|
17 |
-
return tokenizer, model
|
18 |
|
19 |
-
|
|
|
20 |
|
21 |
# Supported languages
|
22 |
languages = ['English', 'French', 'Spanish', 'Hindi', 'Punjabi']
|
@@ -45,14 +32,22 @@ def main():
|
|
45 |
st.success("Translation:")
|
46 |
st.write(translation)
|
47 |
|
48 |
-
|
49 |
def translate_text(text, input_language, output_language):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
main()
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import openai
|
3 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Set up OpenAI API key
|
6 |
+
openai.api_key = os.getenv("OPENAI_KEY")
|
7 |
|
8 |
# Supported languages
|
9 |
languages = ['English', 'French', 'Spanish', 'Hindi', 'Punjabi']
|
|
|
32 |
st.success("Translation:")
|
33 |
st.write(translation)
|
34 |
|
35 |
+
Function to translate text using GPT-3.5
|
36 |
def translate_text(text, input_language, output_language):
|
37 |
+
try:
|
38 |
+
response = openai.ChatCompletion.create(
|
39 |
+
model="gpt-3.5-turbo",
|
40 |
+
messages=[
|
41 |
+
{"role": "system", "content": f"You translate text from {input_language} to {output_language}"},
|
42 |
+
{"role": "user", "content": text}
|
43 |
+
],
|
44 |
+
max_tokens=5000
|
45 |
+
)
|
46 |
+
translation = response.choices[0].message['content'].strip()
|
47 |
+
return translation
|
48 |
+
|
49 |
+
except Exception as e:
|
50 |
+
return f"Error: {str(e)}"
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
main()
|