aminahmed78 commited on
Commit
14e31fa
·
verified ·
1 Parent(s): ccd9827

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -67
app.py CHANGED
@@ -3,9 +3,6 @@ import os
3
  from transformers import pipeline
4
  from langdetect import detect
5
  from groq import Groq
6
- import torch
7
- print(torch.__version__)
8
- print("CUDA available:", torch.cuda.is_available()) # Check if GPU is available
9
 
10
  # Load Hugging Face token from environment
11
  HF_TOKEN = os.environ.get("homeo_doc")
@@ -15,9 +12,6 @@ if not HF_TOKEN:
15
  # Initialize translation pipeline
16
  translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", token=HF_TOKEN)
17
 
18
- # Initialize Groq client for homeopathic advice
19
- groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
20
-
21
  # Language code mapping for NLLB-200
22
  LANG_CODE_MAP = {
23
  'en': 'eng_Latn', # English
@@ -32,68 +26,22 @@ def translate_text(text, target_lang='eng_Latn'):
32
  """Translate text using NLLB-200"""
33
  try:
34
  source_lang = detect(text)
35
- source_code = LANG_CODE_MAP.get(source_lang, 'eng_Latn')
36
- translation = translator(text)[0]['translation_text']
37
- return translation
38
- except Exception as e:
39
- st.error(f"Translation error: {str(e)}")
40
- return text
41
 
42
- def get_homeopathic_advice(symptoms):
43
- """Get medical advice using Groq API"""
44
- try:
45
- response = groq_client.chat.completions.create(
46
- model="llama3-70b-8192",
47
- messages=[{
48
- "role": "user",
49
- "content": f"Act as a homeopathic expert. Suggest remedies for: {symptoms}"
50
- }],
51
- temperature=0.3
52
  )
53
- return response.choices[0].message.content
54
- except Exception as e:
55
- return f"Error: {str(e)}"
56
-
57
- # Streamlit UI
58
- st.set_page_config(page_title="Homeo Advisor", page_icon="🌿")
59
- st.title("🌍 Multilingual Homeopathic Advisor")
60
-
61
- # Chat interface
62
- if "messages" not in st.session_state:
63
- st.session_state.messages = []
64
-
65
- for message in st.session_state.messages:
66
- with st.chat_message(message["role"]):
67
- st.markdown(message["content"])
68
-
69
- if prompt := st.chat_input("Describe symptoms in any language"):
70
- st.session_state.messages.append({"role": "user", "content": prompt})
71
-
72
- # Process input
73
- with st.spinner("Analyzing..."):
74
- # Translate input to English
75
- english_input = translate_text(prompt)
76
-
77
- # Get medical advice
78
- english_advice = get_homeopathic_advice(english_input)
79
-
80
- # Translate back to original language
81
- source_lang = detect(prompt)
82
- translated_advice = translate_text(english_advice)
83
-
84
- # Format response
85
- final_response = f"""
86
- **English Recommendation:**
87
- {english_advice}
88
 
89
- **Translated Recommendation ({source_lang.upper()}):**
90
- {translated_advice}
91
- """
92
-
93
- # Display response
94
- with st.chat_message("assistant"):
95
- st.markdown(final_response)
96
- st.session_state.messages.append({"role": "assistant", "content": final_response})
97
 
98
- # Disclaimer
99
- st.caption("⚠️ This is not medical advice. Consult a professional.")
 
 
 
 
3
  from transformers import pipeline
4
  from langdetect import detect
5
  from groq import Groq
 
 
 
6
 
7
  # Load Hugging Face token from environment
8
  HF_TOKEN = os.environ.get("homeo_doc")
 
12
  # Initialize translation pipeline
13
  translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", token=HF_TOKEN)
14
 
 
 
 
15
  # Language code mapping for NLLB-200
16
  LANG_CODE_MAP = {
17
  'en': 'eng_Latn', # English
 
26
  """Translate text using NLLB-200"""
27
  try:
28
  source_lang = detect(text)
29
+ source_code = LANG_CODE_MAP.get(source_lang, 'eng_Latn') # Detect source language
 
 
 
 
 
30
 
31
+ translation = translator(
32
+ text,
33
+ src_lang=source_code, # Pass source language
34
+ tgt_lang=target_lang # Pass target language
 
 
 
 
 
 
35
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ return translation[0]['translation_text']
38
+
39
+ except Exception as e:
40
+ st.error(f"Translation error: {str(e)}")
41
+ return text
 
 
 
42
 
43
+ # Test function
44
+ if __name__ == "__main__":
45
+ test_text = "یہ ایک آزمائشی جملہ ہے۔" # Urdu sample text
46
+ translated = translate_text(test_text, "eng_Latn")
47
+ print("Translated:", translated)